小编典典

PHAsset到UIImage

swift

我正在尝试从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。

谢谢。


阅读 467

收藏
2020-07-07

共1个答案

小编典典

这样做是我需要做的,以防万一任何人也需要它。

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
}
2020-07-07