0

我正在开发一个应用程序,因为我的工作是使用AssetsLibrary. 但我面临以下问题:

  1. 当我尝试访问连拍照片时
  2. 尝试从 iTunes/iCloud 访问同步的照片。

所以请帮助我如何使用AssetsLibrary.

4

1 回答 1

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;
}
于 2017-04-06T05:53:35.680 回答