gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:04:13 25 4
gpt4 key购买 nike

好吧,我已经研究这个问题有一段时间了,但我似乎无法解决它。

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

选择图像后,完成按钮将被激活。我有一个 UIImageView,当我按下 func didButtonDidPress(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)



}

其中标记有错误。有人能帮我一下吗?!谢谢

最佳答案

我从 PHAsset 获取 UIImage,如下所示:

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

关于swift - 如何使用按钮单击功能将 PHAsset 转换为 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36946624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com