Can get image from PHAsset of library

8.8k views Asked by At

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

2

There are 2 answers

6
jignesh Vadadoriya On BEST ANSWER

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)
}
0
Mahesh Chaudhari On

Get video URL from PHAsset in Swift 4.2 from didFinishPickingAssets in QBImagePickerController

let imagePicker = QBImagePickerController()
imagePicker.delegate = self
imagePicker.numberOfColumnsInPortrait = 2
imagePicker.maximumNumberOfSelection = 2
imagePicker.allowsMultipleSelection = true
imagePicker.mediaType = .video

self.present(imagePicker, animated: true, completion: nil)

func qb_imagePickerController(_ imagePickerController: QBImagePickerController!, didFinishPickingAssets assets: [Any]!) {
    for asset in assets
    {
        PHCachingImageManager().requestAVAsset(forVideo: asset as! PHAsset, options: nil, resultHandler: {(asset, audioMix, info) -> Void in
            if asset != nil {
                let avasset = asset as! AVURLAsset
                let urlVideo = avasset.url
                yourarray.add(urlVideo as URL)
            }

        })

    }


    imagePickerController.dismiss(animated: true) {
        //You can access video urls here
        print("Total Videos:\(yourarray.count)")
    }

}