获取UIImage的大小(字节长度)而不是高度和宽度

时间:2009-08-18 21:43:43

标签: iphone uiimage

我正试图获得UIImage的长度。不是图像的宽度或高度,而是数据的大小。

11 个答案:

答案 0 :(得分:71)

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

答案 1 :(得分:32)

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

答案 2 :(得分:17)

使用UIImage的CGImage属性。然后使用CGImageGetBytesPerRow *
的组合 CGImageGetHeight,添加大小的UIImage,你应该在实际大小的几个字节之内。

这将返回未压缩的图像大小,如果你想将它用于malloc以准备位图操作(假设RGB字节为3字节,Alpha为1字节):

int height = image.size.height,
    width = image.size.width;
int bytesPerRow = 4*width;
if (bytesPerRow % 16)
    bytesPerRow = ((bytesPerRow / 16) + 1) * 16;
int dataSize = height*bytesPerRow;

答案 3 :(得分:13)

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

答案 4 :(得分:7)

Swift 中的示例:

let img: UIImage? = UIImage(named: "yolo.png")
let imgData: NSData = UIImageJPEGRepresentation(img, 0)
println("Size of Image: \(imgData.length) bytes")

答案 5 :(得分:3)

以下是获得答案的最快,最干净,最一般,最不容易出错的方法。在类别UIImage+MemorySize中:

#import <objc/runtime.h>

- (size_t) memorySize
{
  CGImageRef image = self.CGImage;
  size_t instanceSize = class_getInstanceSize(self.class);
  size_t pixmapSize = CGImageGetHeight(image) * CGImageGetBytesPerRow(image);
  size_t totalSize = instanceSize + pixmapSize;
  return totalSize;
}

或者如果你只想要实际的位图而不是UIImage实例容器,那么它真的很简单:

- (size_t) memorySize
{
  return CGImageGetHeight(self.CGImage) * CGImageGetBytesPerRow(self.CGImage);
}

答案 6 :(得分:1)

我不确定你的情况。如果你需要实际的字节大小,我认为你不这样做。您可以使用UIImagePNGRepresentation或UIImageJPEGRepresentation来获取图像压缩数据的NSData对象。

我认为您想获得未压缩图像(像素数据)的实际大小。您需要将UIImage *或CGImageRef转换为原始数据。这是将UIImage转换为IplImage(来自OpenCV)的示例。您只需要分配足够的内存并将指针传递给CGBitmapContextCreate的第一个arg。

UIImage *image = //Your image
CGImageRef imageRef = image.CGImage;

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
IplImage *iplimage = cvCreateImage(cvSize(image.size.width, image.size.height), IPL_DEPTH_8U, 4);
CGContextRef contextRef = CGBitmapContextCreate(iplimage->imageData, iplimage->width, iplimage->height,
                                                iplimage->depth, iplimage->widthStep,
                                                colorSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrderDefault);
CGContextDrawImage(contextRef, CGRectMake(0, 0, image.size.width, image.size.height), imageRef);
CGContextRelease(contextRef);
CGColorSpaceRelease(colorSpace);

IplImage *ret = cvCreateImage(cvGetSize(iplimage), IPL_DEPTH_8U, 3);
cvCvtColor(iplimage, ret, CV_RGBA2BGR);
cvReleaseImage(&iplimage);

答案 7 :(得分:1)

斯威夫特3:

let image = UIImage(named: "example.jpg")
if let data = UIImageJPEGRepresentation(image, 1.0) {
    print("Size: \(data.count) bytes")
}

答案 8 :(得分:0)

如果需要以人类可读的形式,我们可以使用ByteCountFormatter

class MyCustomException { // Doesn't derive
 ///
};

if let data = UIImageJPEGRepresentation(image, 1.0) { let fileSizeStr = ByteCountFormatter.string(fromByteCount: Int64(data.count), countStyle: ByteCountFormatter.CountStyle.memory) print(fileSizeStr) } 是您需要的数字格式。

答案 9 :(得分:0)

SWIFT 4 +

let imgData = image?.jpegData(compressionQuality: 1.0)
debugPrint("Size of Image: \(imgData!.count) bytes")

您可以使用此技巧来找出图像大小。

答案 10 :(得分:0)

斯威夫特 4 和 5:

extension UIImage {
    var sizeInBytes: Int {
        guard let cgImage = self.cgImage else {
            // This won't work for CIImage-based UIImages
            assertionFailure()
            return 0
        }
        return cgImage.bytesPerRow * cgImage.height
    }
}
相关问题