I'm trying to get the length of a UIImage. Not the width or height of the image, but the size of the data.
Thank you.
I'm trying to get the length of a UIImage. Not the width or height of the image, but the size of the data.
Thank you.
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.
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.
Haven't tried it myself, but it seems logical.
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);