Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

}
like image 549
eddit Avatar asked Jul 31 '15 18:07

eddit


1 Answers

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...
        })
like image 179
Stan Mots Avatar answered Nov 15 '22 16:11

Stan Mots