可以从库的PHAsset获取图像

时间:2016-12-27 06:15:46

标签: ios swift phasset

我正在使用QBImagePickerController一次选择多张图片。

所以,这是我的整个代码

我正在使用此代码提交imagepickerController

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

因此,当我选择多个图像并单击完成时,此方法称为

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

例如,我选择一个图像,然后按照这样打印

<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 

从这个我怎样才能获得图像并将其设置为collectionview?

我从中获取了fileName,但不值得从中设置图像。

我使用filePathURL,fileURL,absoluteURL,但没有发生任何事情,它崩溃了

所以请帮帮我吧谢谢

2 个答案:

答案 0 :(得分:6)

您需要使用requestImageForAsset来获取UIImage。 你可以像这样获得图像

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

答案 1 :(得分:1)

从QBImagePickerController中的didFinishPickingAssets从Swift 4.2中的PHAsset获取视频URL

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

}