Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can get image from PHAsset of library

Tags:

ios

swift

phasset

I am using QBImagePickerController for selecting multiple images at a time.

So, here is my whole code

I am presenting imagepickerController with this code

let imagePickerController = QBImagePickerController()
imagePickerController.delegate = self
imagePickerController.allowsMultipleSelection = true
imagePickerController.mediaType = .Image
self.presentViewController(imagePickerController, animated: 
true, completion: nil)

so when I choose multiple images and click done, This method called

func qb_imagePickerController(imagePickerController: QBImagePickerController!, didFinishPickingAssets assets: [AnyObject]!) {
        for asset in assets {
            print(asset.fileName)   
        }
        self.dismissViewControllerAnimated(true, completion: nil)
    }

For example, I select one image then it prints like this

<PHAsset: 0x7fc55d954500> 6006CE57-81FE-4DC0-8C52-5DB43CE7638D/L0/001 mediaType=1/0, sourceType=1, (1920x1080), creationDate=2016-05-26 09:15:34 +0000, location=0, hidden=0, favorite=0 

From this how can I get image and set it into collectionview?

I get fileName from it but its not worthy to set image from it.

I use filePathURL, fileURL, absoluteURL but nothing happened it crashed

So please help me with it
Thank you

like image 608
Jitendra Modi Avatar asked Dec 27 '16 06:12

Jitendra Modi


1 Answers

You need to use requestImageForAsset to get UIImage. You can get image like this way

func qb_imagePickerController(imagePickerController: QBImagePickerController!, didFinishPickingAssets assets: [AnyObject]!) {


    let requestOptions = PHImageRequestOptions()
    requestOptions.resizeMode = PHImageRequestOptionsResizeMode.Exact
    requestOptions.deliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat
    // this one is key
    requestOptions.synchronous = true

    for asset in assets
    {
        if (asset.mediaType == PHAssetMediaType.Image)
        {

            PHImageManager.defaultManager().requestImageForAsset(asset as! PHAsset, targetSize: PHImageManagerMaximumSize, contentMode: PHImageContentMode.Default, options: requestOptions, resultHandler: { (pickedImage, info) in

                self.yourImageview.image = pickedImage // you can get image like this way

            })

        }
    }
    imagePickerController.dismissViewControllerAnimated(true, completion: nil)
}
like image 132
jignesh Vadadoriya Avatar answered Sep 18 '22 02:09

jignesh Vadadoriya