gpt4 book ai didi

iOS - 获取从照片卷轴中选择的图像的文件大小

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:02:37 27 4
gpt4 key购买 nike

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

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

编辑

通过视频获取 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];

最佳答案

仅针对图片数据选择:

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]);

希望对您有所帮助。

下面的方法是通用的,它适用于图像和视频:

像这样的东西应该注意查找从 UIImagePickerController

返回的选定图像或视频的文件大小
- (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];
}

关于iOS - 获取从照片卷轴中选择的图像的文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27244714/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com