PhotoKit does not return the largest available on device image when requesting a larger image size

Originator:michaelab1
Number:rdar://25181601 Date Originated:2016-03-16
Status:Open Resolved:
Product:iOSSDK Product Version:9.3
Classification:Serious Bug Reproducible:Always
 
Summary:
When using PHImageManager or PHCachingImageManager to request large images using the PHImageRequestOptionsDeliveryMode.Opportunistic option the first (low quality) image that is returned is not the largest image that is currently available on the device.

If you request an image using PHImageManager.
requestImageForAsset(_:targetSize:contentMode:options:resultHandler:) 
with the option PHImageRequestOptionsDeliveryMode.Opportunistic 
and a targetSize of *up to* 256x256, PhotoKit first returns a tiny (60x45) thumbnail and almost instantly afterwards returns the largest thumbnail currently available on device (without needing to download anything from iCloud). These are typically 342x256 images.

However, if you make exactly the same request with a larger targetSize (eg 512x512), PhotoKit also first returns the tiny 60x45 thumbnail, but it then waits until it can obtain the full size image from iCloud Photo Library, even though the largest on device image is 342x256. This can be proven by using the networkAccessAllowed option set to false. With this option and a 256x256 targetSize, the larger (342x256) thumbnail is still returned so it must be available on the device.

Even on the Simulator this effect can be seen although here when requesting 512x512 the full size image is eventually returned as it is stored on disk.

When using the Opportunistic option, and a large targetSize, PhotoKit should always return the largest available image currently on the device before trying to download a larger image from iCloud Photo Library.

Steps to Reproduce:
Note: the ideal setup for demonstrating the problem is an iOS Device with iCloud Photo Library enabled with "Optimize Storage" option such that only thumbnails are stored on the device, not full size photos. The problem can be demonstrated on the iOS Simulator, but not so well.

1. Get the project PhotoKitBug from https://github.com/mluisbrown/PhotoKitBug which will demonstrate the bug (zip also attached to bug report).
2. Build and run the PhotoKitBug app on the test device from Xcode (so you can see the debug console output).
3. When prompted at app startup, Allow the app access to Photos.
4. Tap the 256x256 button in the top left corner.
5. The debug console will show that a 60x45 image is loaded and then almost immediately afterwards, a larger 342x256 image.
6. Now tap the 512x512 button in the top right corner.
7. The debug console shows the 60x45 thumbnail is loaded first, and then no larger image is ever loaded (if you are running on a device with iCloud Photo Library enabled as mentioned above). We are using networkAccessAllowed = false. If you are running on the simulator, the full size image is eventually loaded after a delay of a second or so, but you will still note that the 342x256 thumbnail loaded when the smaller image was requested is never provided as an intermediate.


Expected Results:
For a large targetSize, the largest available on device image should always be returned *before* attempting to download any full size image from iCloud Photo Library (or even from disk storage).

Actual Results:
For a large targetSize, only a tiny 60x45 thumbnail is returned even though a larger image is available on the device.

Version:
iOS 9.3b5, 13E5231a

Comments

A StackOverflow question I wrote about this issue has 39 upvotes (at the time of writing) and no answers. It's clearly an issue for a lot of people: http://stackoverflow.com/q/32342522/368085


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!