iosswiftimagephotosphasset

PHAssetManager ignoring targetSize when requesting image?


I have a list of PHAssets that I need to get images from of a certain size.

To test this out, I give the size to be the size of the device screen

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

I need the resulting photos to be cut to the size I specify, but they're off by over 2000 pixels. How do I resolve this?


Solution

  • option.resizeMode = .exact
    

    Per Apple:

    To serve your request more quickly, Photos may provide an image that is slightly larger than the target size—either because such an image is already cached or because it can be generated more efficiently.

    Since you want a specific size, then set the above option to force the request to respect your size.

    Option

    case exact
    

    Photos resizes the image to match the target size exactly.