将 PHAsset 转换为 UIImage 并将其显示给用户

Posted

技术标签:

【中文标题】将 PHAsset 转换为 UIImage 并将其显示给用户【英文标题】:Converting PHAsset to UIImage and displaying it to user 【发布时间】:2017-03-09 11:48:49 【问题描述】:

我正在使用BSImagePicker Image Picker 控制器 (See here)

图像选择器控制器返回 PHAsset 而不是 UIImage,所以我发现了如何使用此函数将 PHAsset 转换为 UIImage:

PHAsset to UIImage

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

问题是,我想返回PHImageManagerMaximumSize 的targetSize。虽然发生这种情况时,我得到一个errorrThis application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.

当我将targetSize设置为CGSize(width: 100, height: 100)时不会出现错误

【问题讨论】:

【参考方案1】:

你需要做的就是避免你的错误是强制应用程序修改主线程上的自动布局。例如,您可以在设置图像时在主队列中使用调度程序。 例如:

let image = self.getAssetThumbnail(asset: <YourPHAsset>)

        DispatchQueue.main.async 

            self.imageView.image = image
        

当你使用targetSize...奇怪...

【讨论】:

以上是关于将 PHAsset 转换为 UIImage 并将其显示给用户的主要内容,如果未能解决你的问题,请参考以下文章

如何将 PHAsset 与 UIImage 进行比较

如何将 UIImage 转换为 .JPEG 格式并将其发送到服务器?

Swift 3 将数据转换为 AVAsset 或 PHAsset

Swift 在 CollectionView 中结合 PHAsset 和 UIImage

在显示之前将 uiview 转换为 uiimage

将 UIImage 转换为 Keras 模型的 MLMultiArray