如何使用按钮单击功能将PHAsset转换为UIImage

时间:2016-04-29 20:06:14

标签: swift uiimageview uiimage swift2 phasset

好的,所以我已经有一段时间了,我似乎无法解决它。

我有一个ImagePicker,它将图像捆绑到PHAssest中。它来自https://github.com/hyperoslo/ImagePicker

选择图像后,将激活完成按钮。我有一个UIImageView,当我按下func doneButtonDidPress(images: [UIImage])时,会将PHAsset转换为UIImage并将其加载到ImageView中。目前,这是我的代码不起作用:

// CONVERT PHASSET TO UIIMAGE
func getAsset(asset: PHAsset) -> UIImage {
let manager = PHImageManager.defaultManager()
let option = PHImageRequestOptions()
var image = UIImage()
option.synchronous = true
manager.requestImageForAsset(asset, targetSize: CGSize(width: 100.0, height: 100.0), contentMode: .AspectFit, options: option, resultHandler: {(result, info)->Void in
    image = result!
})
return image
}




func doneButtonDidPress(images: [UIImage]) {



self.imagePickerController.dismissViewControllerAnimated(true, completion: nil)
var selectedImg = imagePickerController.stack.assets

getAsset(selectedImg)
self.SelectedImageView.image = UIImage(named: selectedImg)



}

哪些标记有错误。有人能帮我一下吗?!三江源

1 个答案:

答案 0 :(得分:1)

我从UIImage获得PHAsset,如:

func getAsset(asset: PHAsset) -> UIImage {
var image = UIImage()
let imgManager = PHImageManager.defaultManager()
            let requestOptions = PHImageRequestOptions()
            requestOptions.synchronous = true
            imgManager.requestImageForAsset(asset, targetSize: CGSizeMake(300.0, 300.0), contentMode: PHImageContentMode.AspectFit, options: requestOptions, resultHandler: { (img, _) in
                image = img
            })
return image
}

此代码经过测试和测试目前在我的申请中工作。

完成按钮方法应该是:

func doneButtonDidPress(image: [UIImage]) {
self.imagePickerController.dismissViewControllerAnimated(true, completion: nil)
var selectedImg = imagePickerController.stack.assets.first
self.selectedImageView.image = getAsset(selectedImg)
}