ios - 将 PHAsset 转换为 UIImage 后设置 UiButton 图像
问题描述
嗨,我正在使用以下代码从照片库中获取最后一张图像,然后将其设置为按钮,但结果显示默认颜色为灰色而不是图像
func loadLastImageThumb(completion: @escaping (UIImage) -> ()) {
let imgManager = PHImageManager.default()
let fetchOptions = PHFetchOptions()
// fetchOptions.fetchLimit = 1
fetchOptions.sortDescriptors = [NSSortDescriptor(key:"creationDate", ascending: true)]
let fetchResult = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: fetchOptions)
if let last = fetchResult.lastObject {
let options = PHImageRequestOptions()
options.deliveryMode = .fastFormat
options.resizeMode = .exact
options.isSynchronous = true
options.version = .original
print(last)
imgManager.requestImage(for: last, targetSize: gallery.bounds.size, contentMode: PHImageContentMode.aspectFit, options: options, resultHandler: { (image, _) in
if let image = image {
completion(image)
print(image)
print(image.size)
print(self.gallery.bounds.size)
}
})
}
}
//在视图中确实加载了
self.loadLastImageThumb { [weak self] (image) in
DispatchQueue.main.async {
self?.gallery.setImage(image, for: .normal)
}
}
解决方案
代码将是
self?.gallery.setBackgroundImage(image, for: .normal)
推荐阅读
- listview - Xamarin 表单:如何将列表视图滚动行为更改为从下到上?
- android-bluetooth - 设置蓝牙自动连接的优先级
- python - 什么决定了可以在 DjangoRestFramework 中更新哪些字段
- c# - RSA - 可以加密,但加密的消息太大而无法解密
- javascript - 在运行时创建一组 gulp 任务
- javascript - 如何通过值Javascript从对象数组中获取键
- typescript - 在 VS 代码中调试 typescript 测试
- python - PyCharm:让“在控制台中执行代码”停止询问我是哪个控制台
- html - 在多个 div 上显示/隐藏而不定义 div 元素 id
- python - 如何动态地将数据传递给 scipy.stats Levene 的测试函数?