【问题标题】:PHAssetManager ignoring targetSize when requesting image?请求图像时 PHAssetManager 忽略 targetSize?
【发布时间】:2018-11-21 04:16:29
【问题描述】:

我有一个PHAssets 列表,我需要从中获取一定大小的图像。

为了测试这一点,我将尺寸设置为设备屏幕的尺寸

        let manager = PHImageManager.default()
        let option = PHImageRequestOptions()
        option.isSynchronous = true

        for asset in assets {
            manager.requestImage(for: asset, targetSize: CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height), contentMode: .aspectFit, options: option, resultHandler: {(result, info)->Void in

                //prints the proper bounds for a screen
                print("width and height \(UIScreen.main.bounds.width) \(UIScreen.main.bounds.height)")

                //prints 2304.0 3072.0 for width and height respectively
                print("result size \(result!.size.width) \(result!.size.height)\n")

                self.photos.append(result!)
            })
        }

我需要将生成的照片切割成我指定的尺寸,但它们相差超过 2000 像素。我该如何解决这个问题?

【问题讨论】:

  • 我目前无法访问文档,但我似乎记得当您同步获取图像时,每个图像可能会获得多个结果。回调中的信息可以告诉你给定的结果是否是最终结果。
  • @matt 它的意思正是它听起来的样子。实际图像的大小比请求的大小大 2000 多像素 看看代码中每个打印语句上方的 cmets。打印的值就在那里
  • @rmaddy 不,不适用于isSynchronous=true
  • @matt,它在后台线程中......
  • @matt 对。当它是异步的时候就是我所说的。

标签: ios swift image photos phasset


【解决方案1】:
option.resizeMode = .exact

Per Apple:

为了更快地满足您的要求,照片可能会提供一张图片, 比目标尺寸略大——要么是因为这样的图像 已经被缓存或者因为它可以更有效地生成。

既然你想要一个特定的尺寸,那么设置上面的选项来强制请求尊重你的尺寸。

Option

case exact

照片会调整图像大小以完全匹配目标大小。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 2014-02-14
    • 2013-03-07
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 2020-01-17
    相关资源
    最近更新 更多