• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 13:09:07 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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



Best Answer-推荐答案


仅用于图像数据选择:

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)imagePickerControllerUIImagePickerController *)picker didFinishPickingMediaWithInfoNSDictionary *)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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap