【问题标题】:Setting UiButton image after converting PHAsset to UIImage将 PHAsset 转换为 UIImage 后设置 UiButton 图像
【发布时间】:2019-02-15 17:05:12
【问题描述】:

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

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)
    }
}

【问题讨论】:

  • 你是在主线程上做的吗?
  • 从您的打印语句中,我猜您已经尝试调试并发现您正在获取图像,对吗?它是否显示在常规 UIImageView 中?
  • 我可以使用上面的代码获取图像对象。似乎问题在于UIImageView 上显示的图像。检查图像视图的出口,并检索图像大小(如果不应该是 0, 0)。
  • @AnkitJayaswal 它不在图像视图中,它是按钮的出口,作为 m 设置按钮的图像
  • @dfd 这是我打印 uiimage 时得到的结果 , {68, 120}

标签: ios swift uibutton uiimage phasset


【解决方案1】:

代码将是 self?.gallery.setBackgroundImage(image, for: .normal)

【讨论】:

  • 你先生,拯救了我的一天。
猜你喜欢
  • 1970-01-01
  • 2016-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多