iOS – 获取从照片卷轴中select的图像的文件大小

我正在构build一个应用程序,允许用户从他们的设备中select照片和video,并将其上传到服务器。 试图获得每个项目的文件大小(以字节为单位)select,任何人都可以帮我吗?

if ([dict objectForKey:UIImagePickerControllerMediaType] == ALAssetTypePhoto){ // image file if ([dict objectForKey:UIImagePickerControllerOriginalImage]){ NSURL* urlPath=[dict objectForKey:@"UIImagePickerControllerReferenceURL"]; item = [BundleItem itemWithPath:urlPath AndDescription:nil]; item.itemImage = [dict objectForKeyedSubscript:UIImagePickerControllerOriginalImage]; item.itemType = 1; // image item.itemSize = // what do I need here?? [m_items addObject:item]; } } else if ([dict objectForKey:UIImagePickerControllerMediaType] == ALAssetTypeVideo){ // video file if ([dict objectForKey:UIImagePickerControllerOriginalImage]){ NSURL* urlPath=[dict objectForKey:@"UIImagePickerControllerReferenceURL"]; item = [BundleItem itemWithPath:urlPath AndDescription:nil]; item.itemImage = [dict objectForKeyedSubscript:UIImagePickerControllerOriginalImage]; item.itemType = 2; // video item.itemSize = // what do I need here?? [m_items addObject:item]; } } 

编辑

获取带有video的NSCocaoErrorDomain 256:

  NSURL* urlPath=[dict objectForKey:@"UIImagePickerControllerReferenceURL"]; item = [BundleItem itemWithPath:urlPath AndDescription:nil]; item.itemImage = [dict objectForKeyedSubscript:UIImagePickerControllerOriginalImage]; item.itemType = 2; // video //Error Container NSError *attributesError; NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[urlPath path] error:&attributesError]; NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize]; long fileSize = [fileSizeNumber longValue]; item.itemSize = fileSize; [m_items addObject:item]; 

仅用于图像数据select:

 item.itemImage = (UIImage*)[info valueForKey:UIImagePickerControllerOriginalImage]; NSData *imgData = UIImageJPEGRepresentation(item.itemImage, 1); //1 it represents the quality of the image. NSLog(@"Size of Image(bytes):%d",[imgData length]); 

希望这会帮助你。

下面的方法是一般化的,它将适用于图像和video:

像这样的东西应该小心查找从UIImagePickerController返回的选定图像或video的文件大小

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { NSURL *videoUrl=(NSURL*)[info objectForKey:UIImagePickerControllerMediaURL]; //Error Container NSError *attributesError; NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[videoUrl path] error:&attributesError]; NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize]; long long fileSize = [fileSizeNumber longLongValue]; }