嗨我正在使用以下代码从照片库中获取最后一个图像,然后将其设置为按钮,但结果显示默认颜色为灰色而不是图像
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)