gpt4 book ai didi

ios - 将 PHAsset 转换为 UIImage 后设置 UiButton 图像

转载 作者:行者123 更新时间:2023-11-28 14:14:15 25 4
gpt4 key购买 nike

您好,我正在使用以下代码从照片库中获取最后一张图片,然后将其设置为按钮,但结果显示的是默认颜色灰色,而不是图片

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)

}
})
}
}

//在 View 中加载

   self.loadLastImageThumb { [weak self] (image) in
DispatchQueue.main.async {
self?.gallery.setImage(image, for: .normal)
}
}

enter image description here

最佳答案

代码如下self?.gallery.setBackgroundImage(image, for: .normal)

关于ios - 将 PHAsset 转换为 UIImage 后设置 UiButton 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52271746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com