iOS 11: [ImageManager] Unable to load image data

Viewed 9885

After update to iOS 11, photo assets now load slowly and I get this message in console:

[ImageManager] Unable to load image data, /var/mobile/Media/DCIM/103APPLE/IMG_3064.JPG

I use static function to load image:

class func getAssetImage(asset: PHAsset, size: CGSize = CGSize.zero) -> UIImage? {
    let manager = PHImageManager.default()
    let option = PHImageRequestOptions()
    option.isSynchronous = true

    var assetImage: UIImage!
    var scaleSize = size
    if size == CGSize.zero {
        scaleSize = CGSize(width: asset.pixelWidth, height: asset.pixelHeight)
    }

    manager.requestImage(for: asset, targetSize: scaleSize, contentMode: .aspectFit, options: option) { (image, nil) in
        if let image = image {
            assetImage = image
        }
    }
    if assetImage == nil {
        manager.requestImageData(for: asset, options: option, resultHandler: { (data, _, orientation, _) in
            if let data = data {
                if let image = UIImage.init(data: data) {
                    assetImage = image
                }
            }
        })
    }
    return assetImage
}

Request image for asset usually always succeeds, but it prints this message. If I use requestImageData function only, there is no such message, but photos made with Apple camera lose their orientation and I get even more issues while loading big amount of images (I use image slideshow in my app).

Apple always sucks when it comes to updates, maybe someone got a solution how to fix this? It even fails to load an asset, when there is a big list of them in user camera. Switching to requestImageData is not an option for me as it brings nil data frequently now.

I would like to point out, that I call this function only once. It is not used in UITableView etc. I use other code for thumbs with globally initialised manager and options, so assets are definitely not nil or etc. I call this function only when user clicks at certain thumb. When gallery has like 5000 photos, maybe connection to assets is just overloaded and later it can't handle request and crashes? So many questions.

5 Answers

Hey I was having the warning as well and here is what worked for me.

Replacing CGSize(width: asset.pixelWidth, height: asset.pixelHeight) by PHImageManagerMaximumSize in requestImage call removed the warning log

Hope this helps,

Your code has some serious issues. You are saying .isSynchronous = true without stepping into a background thread to do the fetch. That is illegal and is what is causing the slowness. Plus, you are asking for a targetSize without also saying .resizeMode = .exact, which means you are getting much bigger images than you are asking for.

However, the warning you're seeing is irrelevant and can be ignored. It in no way signals a failure of image delivery; it seems to be just some internal message that has trickled up to the console by mistake.

This seems to be a bug with iOS 11, but I found I could work around by setting synchronous option false. I reworked my code to deal with the async delivery. Probably you can use sync(execute:) for quick fix.

Also, I believe the problem only occurred with photos delivered by iCloud sharing.

You can try method "requestImageData" with following options. This worked for me in iOS 11.2 (both on device and simulator).

let options = PHImageRequestOptions()
options.deliveryMode = .highQualityFormat
options.resizeMode = .exact
options.isSynchronous = true

PHImageManager.default().requestImageData(for: asset, options: options, resultHandler: { (data, dataUTI, orientation, info) in
Related