0

I am developing an application, in that my job is to access the photos from the photo library using AssetsLibrary. But I'm facing following problems:

  1. When I try to access the burst photos
  2. Try to access the synced photos from iTunes/iCloud.

So please help me how to access the burst photos and synced photos using AssetsLibrary.

MilanPanchal
  • 2,943
  • 1
  • 19
  • 37
venkat
  • 345
  • 8
  • 21

1 Answers1

0

This method is for Photos framework.

If you already got the PHAsset, you can access the asset's data by following:

// 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
}];

Get assets:

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;
}
tomfriwel
  • 2,575
  • 3
  • 20
  • 47