eddit
eddit

Reputation: 153

Does PHImageManager return images in pixel or point size for 2x and 3x screens?

I'm loading images from the camera roll via PHImageManager, but the returned images are not retina resolution. Do I have to provide a multiplier myself for 2x and 3x or do I have something wrong?

Here is my code:

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

}

Upvotes: 10

Views: 1207

Answers (2)

Stan Mots
Stan Mots

Reputation: 1228

According to my experiments with PHImageManageryou must provide a targetSize in pixels. For example, imagine you wanna request an image with the size 400x800 px. For this case you can set the target size like the following:

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

Regarding your code example, in the Apple's docs stated that:

The coordinates of the frame rectangle are always specified in points.

So to set the correct target size in this case you can use code similar to the following:

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

Upvotes: 10

matt
matt

Reputation: 535935

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.

Upvotes: 2

Related Questions