gpt4 book ai didi

swift - PHAsset 到 UIImage

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

我正在尝试从 PHAsset 创建 UIImage(如缩略图或其他内容),以便我可以将其传递到需要 UIImage 的内容中。我尝试过调整我在SO上找到的解决方案(因为它们都直接将其传递到表格 View 或其他东西中),但我没有成功(可能是因为我做得不对)。

func getAssetThumbnail(asset: PHAsset) -> UIImage {
var retimage = UIImage()
println(retimage)
let manager = PHImageManager.defaultManager()
manager.requestImageForAsset(asset, targetSize: CGSize(width: 100.0, height: 100.0), contentMode: .AspectFit, options: nil, resultHandler: {(result, info)->Void in
retimage = result
})
println(retimage)
return retimage
}

printlns 告诉我 manager.request 行现在没有执行任何操作。我如何让它以 UIImage 的形式提供 Assets 。

谢谢。

最佳答案

这完成了我需要它做的事情,以防有人也需要这个。

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

编辑:Swift 3 更新

func getAssetThumbnail(asset: PHAsset) -> UIImage {
let manager = PHImageManager.default()
let option = PHImageRequestOptions()
var thumbnail = UIImage()
option.isSynchronous = true
manager.requestImage(for: asset, targetSize: CGSize(width: 100, height: 100), contentMode: .aspectFit, options: option, resultHandler: {(result, info)->Void in
thumbnail = result!
})
return thumbnail
}

关于swift - PHAsset 到 UIImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39287071/

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