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
It's a bug and seems to be fixed in the latest iOS 8.2x betas.
It's unbelievable but as of iOS 10.3.3 the bug persists and it looks like it was fixed by only iOS 11...
In order to cover iOS 10 and below I'm using fetchAssets
of PHAsset
with info dictionary returned from didFinishPickingMediaWithInfo
delegate method:
PHAsset.fetchAssets(withALAssetURLs: [info["UIImagePickerControllerReferenceURL"] as! URL], options: nil)
Remember that this returns an array of results.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With