美文网首页
使用PHAsset获取图像或视频文件的大小

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

作者: SwordDevil | 来源:发表于2022-09-29 14:22 被阅读0次

分为图片和视频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获取图像或视频文件的大小

      本文链接:https://www.haomeiwen.com/subject/bkkkartx.html