我试图从PHAsset创建一个UIImage(如缩略图或某物),以便我可以将其传递给需要UIImage的东西。我试过适应我在SO上找到的解决方案(因为它们都直接传递给表视图或某些东西),但是我没有成功(可能是因为我没有做到这一点)。
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给我资产。
谢谢。
这是我需要做的,万一有人也需要这样做。
func getAssetThumbnail(asset: PHAsset) -> UIImage { let manager = PHImageManager.defaultManager() let option = PHImageRequestOptions() var thumbnail = UIImage() option.synchronous = true manager.requestImageForAsset(asset,options: option,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,info)->Void in thumbnail = result! }) return thumbnail }