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

发布于 2024-11-16 10:05:30 字数 334 浏览 2 评论 0原文

我正在尝试从照片库或相机中选取图像。
委托方法:

- (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

明月松间行 2024-11-23 10:05:31

来自:@fbrereto回答

UIImage 的底层数据可能会有所不同,因此对于同一个“图像”,可以具有不同大小的数据。您可以做的一件事是使用 UIImagePNGRepresentationUIImageJPEGRepresentation 获取两者的等效 NSData 构造,然后检查其大小。

来自:@Meet答案

 UIImage *img = [UIImage imageNamed:@"sample.png"];
 NSData *imgData = UIImageJPEGRepresentation(img, 1.0); 
 NSLog(@"Size of Image(bytes):%d",[imgData length]);

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 use UIImagePNGRepresentation or UIImageJPEGRepresentation to get the equivalent NSData constructs for either, then check the size of that.

From:@Meet's answer:

 UIImage *img = [UIImage imageNamed:@"sample.png"];
 NSData *imgData = UIImageJPEGRepresentation(img, 1.0); 
 NSLog(@"Size of Image(bytes):%d",[imgData length]);
夏天碎花小短裙 2024-11-23 10:05:31
- (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];
   }
}
- (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];
   }
}
ゃ人海孤独症 2024-11-23 10:05:30

尝试以下代码:

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

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

Try the following code:

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

int imageSize = imageData.length;
NSLog(@"SIZE OF IMAGE: %i ", imageSize);
臻嫒无言 2024-11-23 10:05:30

我知道这是一个老问题,但创建一个 NSData 对象只是为了获取图像的字节大小可能是一个非常昂贵的操作。图像可以超过 20Mb,并创建相同大小的对象只是为了获得第一个对象的大小...

我倾向于使用此类别:

UIImage+CalculatedSize.h

#import <UIKit/UIKit.h>

@interface UIImage (CalculatedSize)

-(NSUInteger)calculatedSize;

@end

UIImage+CalculatedSize.m< /strong>

#import "UIImage+CalculatedSize.h"

@implementation UIImage (CalculatedSize)

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

@end

您只需导入 UIImage+CalculatedSize.h 并像这样使用它:

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

或者,如果您想避免使用类别:

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

编辑:

这个计算当然没有任何内容要做使用 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

#import <UIKit/UIKit.h>

@interface UIImage (CalculatedSize)

-(NSUInteger)calculatedSize;

@end

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:

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. It relates to underlaying 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文