如何阅读iCloud / iTunes同步照片

时间:2017-04-06 04:03:59

标签: ios iphone alassetlibrary

我正在开发一个应用程序,我的工作是使用AssetsLibrary访问照片库中的照片。但我面临以下问题:

  1. 当我尝试访问连拍照片时
  2. 尝试从iTunes / iCloud访问已同步的照片。
  3. 因此,请使用AssetsLibrary帮助我如何访问连拍照片和同步的照片。

1 个答案:

答案 0 :(得分:0)

此方法适用于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;
}