我想从我的照片流中获取照片。我只有PHAsset的localIdentifier。
对于My Photo Stream相册以外的照片,我使用下面的代码来检索PHAsset。
[PHAsset fetchAssetsWithLocalIdentifiers:@[“localIdentifier”] options:nil].firstObject
localIdentifier就像“1BFC3BA2-AC95-403A-B4FF-B26AFB631581/L0/001
”
它工作正常。但在我的My Photo Stream专辑中,我的PHAsset值为零。
有没有人知道我们如何使用PHAsset的localIdentifier值从My Photo Stream获取原始照片?
答案 0 :(得分:1)
我自己得到了答案。我检索所有照片流照片并将其与特定的localIdentifier进行比较。以下是我的代码。
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];
}];
}];
}]
答案 1 :(得分:1)
请使用此链接http://qiita.com/to_obara/items/e386040abcc93842745b获取swift代码。我按照上面的链接编写了下面的客观c代码。
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]];
}
}];
}
//
}
}];
}
}