如何在 iOS 4.0 中获取 UIImage 的大小(以字节为单位)?
我正在尝试从照片库或相机中选取图像。
委托方法:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
为我提供 UIImage
对象。我需要找到我的应用程序的图像大小(以字节为单位)。
有什么方法可以获取图像的文件类型以及字节大小吗?
任何形式的帮助将不胜感激。
提前致谢
I am trying to pick an image from the photo library or from the camera.
The delegate method:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
Gives me the UIImage
object. I need to find the size of the image in bytes
for my application.
Is there any way I can get the file type of the image and also the size in the bytes?
Any kind of help would be highly appreciated.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
来自:@fbrereto 的 回答:
UIImage
的底层数据可能会有所不同,因此对于同一个“图像”,可以具有不同大小的数据。您可以做的一件事是使用UIImagePNGRepresentation
或UIImageJPEGRepresentation
获取两者的等效NSData
构造,然后检查其大小。来自:@Meet 的 答案:
From:@fbrereto's answer:
The underlying data of a
UIImage
can vary, so for the same "image" one can have varying sizes of data. One thing you can do is useUIImagePNGRepresentation
orUIImageJPEGRepresentation
to get the equivalentNSData
constructs for either, then check the size of that.From:@Meet's answer:
尝试以下代码:
Try the following code:
我知道这是一个老问题,但创建一个 NSData 对象只是为了获取图像的字节大小可能是一个非常昂贵的操作。图像可以超过 20Mb,并创建相同大小的对象只是为了获得第一个对象的大小...
我倾向于使用此类别:
UIImage+CalculatedSize.h
UIImage+CalculatedSize.m< /strong>
您只需导入
UIImage+CalculatedSize.h
并像这样使用它:或者,如果您想避免使用类别:
编辑:
这个计算当然没有任何内容要做使用 JPEG/PNG 压缩。它与底层 CGimage 相关:
在某种程度上,以这种方式检索的大小可以为您提供最坏情况的信息,而无需实际创建昂贵的附加对象。
I know this is an old question but creating a
NSData
object just to get the byte-size of an image can be a really expensive operation. Image can have over 20Mb and creating equally sized object just to get the size of the first one...I tend to use this category:
UIImage+CalculatedSize.h
UIImage+CalculatedSize.m
You simply import the
UIImage+CalculatedSize.h
and use it like this:Or, if you want to avoid using categories:
EDIT:
This calculation of course has nothing to do with JPEG/PNG compression. It relates to underlaying CGimage:
In a way a size retrieved this way gives you a worst-case scenario information without actually creating an expensive additional object.