Can get image from PHAsset of library

Jitendra Modi picture Jitendra Modi · Dec 27, 2016 · Viewed 7.3k times · Source

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

Answer

jignesh Vadadoriya picture jignesh Vadadoriya · Dec 27, 2016

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)
}