I am using a UIImagePickerController
to let the user choose a photo or video to share in the app. When the user chooses a media item in their Library, I execute this code in one of the UIImagePickerController's
delegate methods:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
PHAsset *asset;
if ([info[@"UIImagePickerControllerMediaType"] isEqualToString:@"public.movie"]) {
// Video
asset = [[PHAsset fetchAssetsWithALAssetURLs:@[info[@"UIImagePickerControllerReferenceURL"]] options:nil] lastObject];
} else if ([info[@"UIImagePickerControllerMediaType"] isEqualToString:@"public.image"]) {
// Photo
PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[info[@"UIImagePickerControllerReferenceURL"]] options:nil];
asset = [[PHAsset fetchAssetsWithALAssetURLs:@[info[@"UIImagePickerControllerReferenceURL"]] options:nil] lastObject];
}
}
Both if statements
work fine for both photo and video, except for when you select an item from the album titled "My Photo Stream"
.
When you select an item from "My Photo Stream"
, the returned PHAsset
is always nil
.
I found the following question that appears to have an answer with a working solution: ALAssetsLibrary assetForURL: always returning nil for photos in "My Photo Stream" in iOS 8.1
But the above link uses the AssetsLibrary
framework which is no longer recommended by Apple:
"In iOS 8.0 and later, use the Photos framework instead of the Assets Library framework. The Photos framework provides more features and better performance for working with a user’s photo library. See Photos Framework Reference."
I need to be able to return PHAsset
objects for the media items in the "My Photo Stream"
album. Right now the reference URL that's returned by the UIImagePickerController
in the info
dictionary is a valid URL that logs in the console, but when using this URL, a valid PHAsset
object is never returned.
Here is an example of a reference URL that is returned in the didFinishPickingMediaWithInfo:
delegate method's info dictionary:
assets-library://asset/asset.JPG?id=DCF5C6E5-B4F4-4E61-9C4B-CC63E104BF2B&ext=JPG