如何从我的照片流相册中获取照片

Posted

技术标签:

【中文标题】如何从我的照片流相册中获取照片【英文标题】:How to get photo from My Photo Stream Album 【发布时间】:2016-03-15 06:08:22 【问题描述】:

我想从我的照片流中获取照片。我只有 PHAsset 的 localIdentifier。

对于我的照片流相册以外的照片,我使用以下代码检索 PHAsset。

[PHAsset fetchAssetsWithLocalIdentifiers:@[“localIdentifier”] options:nil].firstObject

localIdentifier 就像'1BFC3BA2-AC95-403A-B4FF-B26AFB631581/L0/001'

它工作正常。但在我的“我的照片流”相册中,PHAsset 的价值为零。

有人知道我们如何使用 PHAsset 的 localIdentifier 值从“我的照片流”中获取原始照片吗?

【问题讨论】:

【参考方案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];
                    ];
                ];
            ]

【讨论】:

【参考方案2】:

请使用此链接http://qiita.com/to_obara/items/e386040abcc93842745b 获取快速代码。我通过上面的链接在下面编写了目标 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]];
                       
                   ];
              
              //
          
            ];
    

【讨论】:

以上是关于如何从我的照片流相册中获取照片的主要内容,如果未能解决你的问题,请参考以下文章

IOS 照片库获取给定照片的相册名称

安卓从手机相册获取照片作为头像缓存地址

如何在文档中显示所有视频以及相册中的照片?

Facebook sdk io 从相册中获取照片

从照片库/相册中获取图像在 ipad 上不起作用

Android开发之获取相册照片和获取拍照照片