Receive files and other asset information without fully loading into memory

I need to get information about the asset without loading it into memory, which can take a lot of time if the asset is very large.

As I will repeat all the assets available on my device, this can take a very long time.

I use the following code to load an object into an object NSData, and also to get some information about it:

- (NSData*)getAssetInfo: (NSUInteger*) assetIndex {

    PHFetchOptions *fetchOptions;
    fetchOptions.sortDescriptors = @[ [NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES], ];
    PHFetchResult *fetchResult = [PHAsset fetchAssetsWithOptions:fetchOptions];

    __block NSData *iData = nil;

    PHAsset *asset = [fetchResult objectAtIndex: assetIndex];

    PHImageManager *imageManager = [PHImageManager defaultManager];
    PHImageRequestOptions *options = [[PHImageRequestOptions alloc]init];
    options.synchronous = YES;
    options.version = PHImageRequestOptionsVersionCurrent;

    @autoreleasepool {
        [imageManager requestImageDataForAsset:asset options:options resultHandler:^(NSData *imgData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
            //NSLog(@"requestImageDataForAsset returned info(%@)", info);
            NSURL *url = [info valueForKey:  @"PHImageFileURLKey"];
            NSString *urlAsString = [url absoluteString];
            gAssetFilename = [urlAsString lastPathComponent];
            gAssetCreationDate = asset.creationDate;
            iData = imgData;
        }];
    }

    if (iData != nil) {
        return iData;
    }
    else{
        NSLog(@"OOPS!");
        return nil;
    }
}

This works, but, as I said, it can take a very long time.

Is it possible to get the size of an asset, as well as other information about assets, without first loading it into memory?

+4
source share

Source: https://habr.com/ru/post/1599688/


All Articles