简体   繁体   English

如何在iOS 4.0+中以字节为单位获取UIImage的大小?

[英]How to get Size of UIImage in Bytes in iOS 4.0+?

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. 给我UIImage对象。 I need to find the size of the image in bytes for my application. 我需要为我的应用程序找到图像的大小(以bytesbytes

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 提前致谢

Try the following code: 请尝试以下代码:

NSData *imageData = [[NSData alloc] initWithData:UIImageJPEGRepresentation((image), 1.0)];

int imageSize = imageData.length;
NSLog(@"SIZE OF IMAGE: %i ", imageSize);

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. 我知道这是一个老问题但是创建一个NSData对象只是为了获得图像的字节大小可能是一个非常昂贵的操作。 Image can have over 20Mb and creating equally sized object just to get the size of the first one... 图像可以有超过20Mb并创建相同大小的对象只是为了获得第一个的大小...

I tend to use this category: 我倾向于使用这个类别:

UIImage+CalculatedSize.h 的UIImage + CalculatedSize.h

#import <UIKit/UIKit.h>

@interface UIImage (CalculatedSize)

-(NSUInteger)calculatedSize;

@end

UIImage+CalculatedSize.m 的UIImage + CalculatedSize.m

#import "UIImage+CalculatedSize.h"

@implementation UIImage (CalculatedSize)

-(NSUInteger)calculatedSize
{    
    return CGImageGetHeight(self.CGImage) * CGImageGetBytesPerRow(self.CGImage);
}

@end

You simply import the UIImage+CalculatedSize.h and use it like this: 您只需导入UIImage+CalculatedSize.h并像这样使用它:

NSLog (@"myImage size is: %u",myImage.calculatedSize);

Or, if you want to avoid using categories: 或者,如果您想避免使用类别:

NSUInteger imgSize  = CGImageGetHeight(anImage.CGImage) * CGImageGetBytesPerRow(anImage.CGImage);

EDIT: 编辑:

This calculation of course has nothing to do with JPEG/PNG compression. 此计算当然与JPEG / PNG压缩无关。 It relates to underlaying CGimage : 它涉及底层CGimage

A bitmap (or sampled) image is a rectangular array of pixels, with each pixel representing a single sample or data point in a source image. 位图(或采样)图像是矩形像素阵列,每个像素表示源图像中的单个样本或数据点。

In a way a size retrieved this way gives you a worst-case scenario information without actually creating an expensive additional object. 在某种程度上,以这种方式检索的大小可以为您提供最坏情况的方案信息,而无需实际创建昂贵的附加对象。

From: @fbrereto 's answer : 来自: @fbrereto回答

The underlying data of a UIImage can vary, so for the same "image" one can have varying sizes of data. UIImage的基础数据可以变化,因此对于相同的“图像”,可以具有不同大小的数据。 One thing you can do is use UIImagePNGRepresentation or UIImageJPEGRepresentation to get the equivalent NSData constructs for either, then check the size of that. 您可以做的一件事是使用UIImagePNGRepresentationUIImageJPEGRepresentation来获取等效的NSData结构,然后检查它的大小。

From: @Meet 's answer : 来自: @Meet回答

 UIImage *img = [UIImage imageNamed:@"sample.png"];
 NSData *imgData = UIImageJPEGRepresentation(img, 1.0); 
 NSLog(@"Size of Image(bytes):%d",[imgData length]);
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)editInfo{
   UIImage *image=[editInfo valueForKey:UIImagePickerControllerOriginalImage];
   NSURL *imageURL=[editInfo valueForKey:UIImagePickerControllerReferenceURL];
   __block long long realSize;

   ALAssetsLibraryAssetForURLResultBlock resultBlock=^(ALAsset *asset)
   {
      ALAssetRepresentation *representation=[asset defaultRepresentation];
      realSize=[representation size];
   };

   ALAssetsLibraryAccessFailureBlock failureBlock=^(NSError *error)
   {
      NSLog(@"%@", [error localizedDescription]);
   };

   if(imageURL)
   {
      ALAssetsLibrary *assetsLibrary=[[[ALAssetsLibrary alloc] init] autorelease];
      [assetsLibrary assetForURL:imageURL resultBlock:resultBlock failureBlock:failureBlock];
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM