PHImageManager是否以像素或点大小返回2x和3x屏幕的图像?

时间:2015-07-31 18:10:01

标签: ios swift photokit

我通过PHImageManager从相机胶卷加载图像,但返回的图像不是视网膜分辨率。我是否必须自己提供2倍和3倍的倍增器,或者我有什么问题?

这是我的代码:

class ReviewableImageView: UIImageView {

    ...unrelated code

    imageRequestOptions = PHImageRequestOptions()
    imageRequestOptions.deliveryMode = .HighQualityFormat
    imageRequestOptions.resizeMode = .Exact

    ...unrelated code

    self.contentMode = .ScaleAspectFit
    self.backgroundColor = UIColor.clearColor()
    self.userInteractionEnabled = true

    ... unrelated code

    func reloadImage(){
        let imageManager = PHCachingImageManager()//PHImageManager()

        imageManager.requestImageForAsset(self.imageAsset,
            targetSize: self.frame.size,
            contentMode: .AspectFit,
            options: imageRequestOptions,
            resultHandler: { (image: UIImage!, info: [NSObject : AnyObject]!) in
                self.image = image
        })
    }

}

2 个答案:

答案 0 :(得分:6)

根据我使用PHImageManager的实验,您必须在像素中提供targetSize。例如,假设您想要一个尺寸为400x800像素的图像。在这种情况下,您可以设置目标大小,如下所示:

 // Set target size.
 let targetSize = CGSizeMake(400, 800)

关于您的代码示例,在Apple's docs中声明:

  

框架矩形的坐标始终以磅为单位指定。

因此,要设置正确的目标大小,您可以使用类似于以下内容的代码:

// Get scale factor associated with the screen. 
let scale = UIScreen.mainScreen().scale

// Request the image.
imageManager.requestImageForAsset(self.imageAsset,
            targetSize: CGSizeMake(self.frame.size.width * scale, self.frame.size.height * scale),
            contentMode: .AspectFit,
            options: imageRequestOptions,
            resultHandler: { (image, info) -> Void in
                // Handle the result here...
        })

答案 1 :(得分:3)

It's hard to tell from your code whether you realize that the resultHandler will be called multiple times. The first time it is called, you may get a low-resolution low-quality image. But eventually it will be called with a correctly scaled image.