如何读取 iCloud/iTunes 同步的照片
Posted
技术标签:
【中文标题】如何读取 iCloud/iTunes 同步的照片【英文标题】:How to get read the iCloud/iTunes synced photos 【发布时间】:2017-04-06 04:03:59 【问题描述】:我正在开发一个应用程序,因为我的工作是使用AssetsLibrary
访问照片库中的照片。但我面临以下问题:
-
当我尝试访问连拍照片时
尝试从 iTunes/iCloud 访问同步的照片。
所以请帮助我如何使用AssetsLibrary
访问连拍照片和同步照片。
【问题讨论】:
【参考方案1】:此方法适用于Photos
框架。
如果您已经获得PHAsset
,您可以通过以下方式访问资产数据:
// For image
PHImageRequestOptions *options = [PHImageRequestOptions new];
options.networkAccessAllowed = YES;
options.version = PHImageRequestOptionsVersionCurrent;
options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
//If comes from iCloud, the progressHandler will be called.
options.progressHandler = ^(double progress, NSError *__nullable error, BOOL *stop, NSDictionary *__nullable info)
NSLog(@"percent:%f, info:%@", progress, info);
// Handle progress
;
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:options resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info)
if (imageData)
// Handle data
];
// For video
PHVideoRequestOptions *options = [PHVideoRequestOptions new];
options.version = PHVideoRequestOptionsVersionCurrent;
options.deliveryMode = PHVideoRequestOptionsDeliveryModeHighQualityFormat;
options.progressHandler = ^(double progress, NSError * _Nullable error, BOOL * _Nonnull stop, NSDictionary * _Nullable info)
NSLog(@"percent:%f, info:%@", progress, info);
// Handle progress
;
[[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info)
AVURLAsset *avurlasset = (AVURLAsset*)asset;
// Handle data
];
获取资产:
PHFetchResult *userLibrary = [PHAssetCollection
fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum
subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary
options:nil];
[userLibrary enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
PHAssetCollection *assetCollection = (PHAssetCollection *)obj;
PHFetchResult *fetchResult = [self getFetchResultInAssetCollection:assetCollection];
[fetchResult enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
PHAsset *asset = (PHAsset *)obj;
// Handle asset
];
];
-getFetchResultInAssetCollection:
-(PHFetchResult *)getFetchResultInAssetCollection:(PHAssetCollection *)assetCollection
PHFetchOptions *options = [[PHFetchOptions alloc] init];
// video and image
options.predicate = [NSPredicate predicateWithFormat:@"mediaType = %d || mediaType = %d", PHAssetMediaTypeImage, PHAssetMediaTypeVideo];
// Sort by creationDate
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
PHFetchResult *fetchResult = [PHAsset fetchAssetsInAssetCollection:assetCollection options:options];
return fetchResult;
【讨论】:
谢谢@tom,但我想使用 AssetLibrary 而不是照片框架来完成这项工作 @venkat 好的,github.com/donobono/DoImagePickerController 可能对你有帮助。 @venkat 这个存储库使用AssetLibrary
in AssetHelper
来处理照片。
,因为他们没有提及有关连拍和同步照片的任何信息以上是关于如何读取 iCloud/iTunes 同步的照片的主要内容,如果未能解决你的问题,请参考以下文章