Loading image from "My Photo Stream" using UIImagePicker results URL and PHAsset on iOS8

Werner Sharp picture Werner Sharp · Oct 27, 2014 · Viewed 11.6k times · Source

I am updated an app from ios7 to ios8 and struggling to get UIImagePicker working to load a picture from the "My Photo Stream" category of photos. The UIImagePicker implementation is standard and I retrieve the URL of the selected photo with:

NSURL *url = [info objectForKey:@"UIImagePickerControllerReferenceURL"];

I then use the new IOS8 APIs for loading this photo:

PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[url] options:nil];

This fails to work. The result.count is zero and no image is found. The URL has an different UUID than if I select the photo from "Moments" or "Camera Roll" but looks well formed. (phone is running 8.1):

url NSURL * @"assets-library://asset/asset.JPG?id=4522DBD1-862C-42BE-AF7C-0D6C76CA7590&ext=JPG"

Anyone have some code to load photos from "My Photo Stream" or a way to disable it for the UIImagePicker display?

Using the older ALAssetsLibrary assetForURL API also fails to load these images.

Answer

gabbler picture gabbler · Oct 29, 2014

I think you can try this to get "my photo stream" album.

PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumMyPhotoStream options:nil];

Photos in My Photo Stream album are uploaded to iCloud and iCloud will manage them efficiently, photos captured within 30 days are there, and I think PHAsset or ALAssetLibrary dealt with locally stored assets. Here is a test I did on my device, I used a imagePicker to get a photo url from stream album, which was

"assets-library://asset/asset.JPG?id=60AE4E50-B835-47EB-B896-5974C21F8C9B&ext=JPG";

And using PHAsset on the same photo I get its localIdentifier:

"60AE4E50-B835-47EB-B896-5974C21F8C9B/L0/001" 

So I think you can strip id from url, and find out which asset's local identifier contains it, then you get the PHAsset you want.