I'm trying to get the length of a UIImage
. Not the width or height of the image, but the size of the data.
问题:
回答1:
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.
回答2:
UIImage *img = [UIImage imageNamed:@"sample.png"]; NSData *imgData = UIImageJPEGRepresentation(img, 1.0); NSLog(@"Size of Image(bytes):%d",[imgData length]);
回答3:
Use the CGImage property of UIImage. Then using a combination of CGImageGetBytesPerRow *
CGImageGetHeight, add in the sizeof UIImage, you should be within a few bytes of the actual size.
This will return the size of the image, uncompressed, if you want to use it for purposes such as malloc in preparation for bitmap manipulation (assuming a 4 byte pixel format of 3 bytes for RGB and 1 for Alpha):
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;
回答4:
- (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]; } }
回答5:
Example in Swift:
let img: UIImage? = UIImage(named: "yolo.png") let imgData: NSData = UIImageJPEGRepresentation(img, 0) println("Size of Image: \(imgData.length) bytes")
回答6:
I'm not sure your situation. If you need the actual byte size, I don't think you do that. You can use UIImagePNGRepresentation or UIImageJPEGRepresentation to get an NSData object of compressed data of the image.
I think you want to get the actual size of uncompressed image(pixels data). You need to convert UIImage* or CGImageRef to raw data. This is an example of converting UIImage to IplImage(from OpenCV). You just need to allocate enough memory and pass the pointer to CGBitmapContextCreate's first 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:
This following is the fastest, cleanest, most general, and least error-prone way to get the answer. In a category UIImage+MemorySize
:
#import - (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; }
Or if you only want the actual bitmap and not the UIImage instance container, then it is truly as simple as this:
- (size_t) memorySize { return CGImageGetHeight(self.CGImage) * CGImageGetBytesPerRow(self.CGImage); }
回答8:
Swift 3:
let image = UIImage(named: "example.jpg") if let data = UIImageJPEGRepresentation(image, 1.0) { print("Size: \(data.count) bytes") }