问题
I want to get Photo from My Photo Stream. I have only localIdentifier of PHAsset.
For photos other than My Photo Stream album I am using below code for retrieve PHAsset.
[PHAsset fetchAssetsWithLocalIdentifiers:@[“localIdentifier”] options:nil].firstObject
localIdentifier is like '1BFC3BA2-AC95-403A-B4FF-B26AFB631581/L0/001
'
it's working fine. But in my My Photo Stream album I am getting nil value of PHAsset.
Does anyone having a idea how we can get original Photo from My Photo Stream using localIdentifier value of PHAsset?
回答1:
I got an answer myself. I retrieve all photostream photos and compare it with particular localIdentifier. Below is my code.
PHFetchOptions *userAlbumsOptions = [PHFetchOptions new];
userAlbumsOptions.predicate = [NSPredicate predicateWithFormat:@"estimatedAssetCount > 0"];
PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumMyPhotoStream options:userAlbumsOptions];
[userAlbums enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx1, BOOL *stop) {
NSLog(@"album title %@", collection.localizedTitle);
PHFetchOptions *fetchOptions = [PHFetchOptions new];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"self.localIdentifier CONTAINS [cd] 'my identifier value'"];
PHFetchResult *assetsFetchResult = [PHAsset fetchAssetsInAssetCollection:collection options:fetchOptions];
[assetsFetchResult enumerateObjectsUsingBlock:^(PHAsset *asset, NSUInteger idx, BOOL *stop1) {
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:option resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
UIImage *imgResult = [UIImage imageWithData:imageData scale:1];
}];
}];
}]
回答2:
Please use this link http://qiita.com/to_obara/items/e386040abcc93842745b for swift code. I have written objective c code below by following the above link.
PHAsset *phAsset;
PHFetchOptions *optionsForFetch = [[PHFetchOptions alloc] init];;
optionsForFetch.includeHiddenAssets = YES;
NSURL *alURL = [info valueForKey: UIImagePickerControllerReferenceURL];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithALAssetURLs: @[alURL] options: nil];
if (fetchResult.count > 0) {
return;
}
NSString *str = alURL.absoluteString;
NSString *localIDFragment = [[[[str componentsSeparatedByString: @"="] objectAtIndex: 1] componentsSeparatedByString: @"&"] objectAtIndex: 0];
PHFetchResult *fetchResultForPhotostream = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumMyPhotoStream options: nil];
if (fetchResultForPhotostream.count > 0) {
PHAssetCollection *photostream = fetchResultForPhotostream [0];
PHFetchResult *fetchResultForPhotostreamAssets = [PHAsset fetchAssetsInAssetCollection:photostream options:optionsForFetch];
if (fetchResultForPhotostreamAssets.count > 0) {
NSIndexSet *i = [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, fetchResultForPhotostreamAssets.count)];
//let i=NSIndexSet(indexesInRange: NSRange(location: 0,length: fetchResultForPhotostreamAssets.count))
[fetchResultForPhotostreamAssets enumerateObjectsAtIndexes: i options: NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
PHAsset *target = obj;
NSString *identifier = target.localIdentifier;
if ([identifier rangeOfString: localIDFragment].length != 0) {
if (target) {
// get photo info from this asset
PHImageRequestOptions * imageRequestOptions = [[PHImageRequestOptions alloc] init];
imageRequestOptions.synchronous = YES;
[[PHImageManager defaultManager]
requestImageDataForAsset:target
options:imageRequestOptions
resultHandler:^(NSData *imageData, NSString *dataUTI,
UIImageOrientation orientation,
NSDictionary *info)
{
if ([info objectForKey:@"PHImageFileURLKey"]) {
// path looks like this -
// file:///var/mobile/Media/DCIM/###APPLE/IMG_####.JPG
NSURL *path = [info objectForKey:@"PHImageFileURLKey"];
selectedImageName = path.absoluteString;
[imagesDictArray addObject: [@{@"image": tempImage, @"imageUrl": selectedImageName} mutableCopy]];
}
}];
}
//
}
}];
}
}
来源:https://stackoverflow.com/questions/36003835/how-to-get-photo-from-my-photo-stream-album