2

Code:

    let requestOptions = PHImageRequestOptions()
    requestOptions.version = .Current
    requestOptions.deliveryMode = .Opportunistic
    requestOptions.resizeMode = .None
    requestOptions.networkAccessAllowed = true
    requestOptions.progressHandler = {
        (progress, error, stop, info) in

        print(progress, info)
    }

    let imageManager = PHImageManager.defaultManager()
    imageManager.requestImageForAsset(asset, targetSize: PHImageManagerMaximumSize, contentMode: .AspectFit, options: requestOptions) {
        …
    }

progressHandler is never called, not even for an image that takes 16 second to download. Why? What did I do wrong?

an0
  • 17,191
  • 12
  • 86
  • 136
  • Perhaps try `.HighQualityFormat` instead? It may be the `.Opportunistic` doesn't report progress...? I've never tried this. Also 16 seconds is not really very long, esp. if you are receiving intermediate images as we go along. – matt Nov 02 '16 at 15:17
  • @matt Just tried that, no help. – an0 Nov 02 '16 at 15:22
  • I've honestly never tried this feature (the `progressHandler`). I just assumed it worked. :( – matt Nov 02 '16 at 16:36
  • I see from this question http://stackoverflow.com/questions/31643440/hide-uiprogressview-after-fetch-for-phasset-is-completed-and-progress-finished-a that it does (or did) sometimes get called. Notice that he's stepping out to the main thread. Could that be the problem? `print` is not thread-safe so perhaps it would help if you tried that. Grasping at straws here...! – matt Nov 02 '16 at 16:39
  • I don't think that's the reason, because I have a breakpoint there and it's never hit. – an0 Nov 02 '16 at 16:43
  • Well, a breakpoint on a background thread is a tricky thing. It's worth at least _trying_ to get on the main thread in your progress handler and see what happens...! – matt Nov 02 '16 at 17:00
  • Tried dispatching to main queue, still no. – an0 Nov 03 '16 at 15:09
  • May I assume you're testing on an actual device? It seems to me you've done everything you can do; you should file a bug with Apple and see what they have to say. – matt Nov 03 '16 at 15:11
  • Yes, tested on 2 devices, 1 with iCloud Photo Library on, 1 off. I'm filing radar now. – an0 Nov 03 '16 at 15:14
  • Have you try to replace the `PHImageManagerMaximumSize` to something like `CGSize(width: asset.pixelWidth, height: asset.pixelHeight)`? This is weird, but in my use cases, the progressHandler is always called. – Jonny Jan 18 '17 at 08:48
  • @Jonny tried, the same. BTW, `pixelWidth` and `pixelHeight` are unusable due to [radar 21045111](http://openradar.appspot.com/21045111). – an0 Jan 19 '17 at 20:09
  • I copy and paste your code into one of my Swift 2 project, and it works just fine: `progressHandler` called multiple times, and photo downloaded. Test with photo store in my iCloud Photo Library. – Jonny Jan 20 '17 at 01:01
  • I didn’t met the problem your radar described. But once a while (iOS 8) I use `pixelWidth` and `pixelHeight` to check whether an image is screenshot, I found my photos synced from Photo Stream are not full-size, some of their resolution are 2048*1536 and it match the iPad screenshot resolution. – Jonny Jan 20 '17 at 01:14
  • @Jonny so it may be data dependent. – an0 Jan 20 '17 at 05:18

1 Answers1

0

The progressHandler will only get called when the Photo Library needs to download a big asset from iCloud. If it's not big enough, or if it is already stored / cached on the device, it won't be called.

Try to put these options in your request:

let requestOptions = PHImageRequestOptions()
requestOptions.networkAccessAllowed = true
requestOptions.resizeMode = .exact
requestOptions.deliveryMode = .highQualityFormat
Frederic Adda
  • 5,905
  • 4
  • 56
  • 71