使用PHAsset获取图像或视频文件的大小

分为图片和视频2部分

  1. 图片
[[PHImageManager defaultManager] requestImageDataForAsset:asset options:nil resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
     float imageSize = imageData.length; //convert to MB
     imageSize = imageSize/(1024*1024.0);
     NSLog(@"%f",imageSize);
 }];
  1. 视频
PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
options.version = PHVideoRequestOptionsVersionOriginal;
[[PHImageManager defaultManager] requestAVAssetForVideo:asset options:options resultHandler:^(AVAsset *asset, AVAudioMix *audioMix, NSDictionary *info) {
    if ([asset isKindOfClass:[AVURLAsset class]]) {
        AVURLAsset* urlAsset = (AVURLAsset*)asset;
        NSNumber *size;
        [urlAsset.URL getResourceValue:&size forKey:NSURLFileSizeKey error:nil];
        NSLog(@"size is %f",[size floatValue]/(1024.0*1024.0)); //size is 43.703005
 }}];

NSURL *referenceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithALAssetURLs:[NSArray arrayWithObject:assetURL] options:nil];
PHAsset *videoAsset = (PHAsset*)fetchResult.firstObject;
PHAssetResource *resource = [[PHAssetResource assetResourcesForAsset:videoAsset] firstObject];
long long originFileSize = [[resource valueForKey:@"fileSize"] longLongValue];
int fileSize = (int)originFileSize;
int fileSize_MB = [[TVUCompareSpaceTool getInstance] convertVideoSizeFromByteToMB:fileSize];

你可能感兴趣的:(使用PHAsset获取图像或视频文件的大小)