Hi
Is there a way to resize an image (Taken from the Camera or UIImagePicker and saved to a temp folder) to 1mb?
Maybe calculating the pixels and than resize it to the calculated pixel size, but dunno how to resize..
big thanks
Hi
Is there a way to resize an image (Taken from the Camera or UIImagePicker and saved to a temp folder) to 1mb?
Maybe calculating the pixels and than resize it to the calculated pixel size, but dunno how to resize..
big thanks
On linux I would use imagemagick to resize the picture, but I suppose you're developping an app on the iPhone ?
I've used this code to resize an image which I found here.
UIImage* resizedImage(UIImage *inImage, CGRect thumbRect)
{
CGImageRef imageRef = [inImage CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
// There's a wierdness with kCGImageAlphaNone and CGBitmapContextCreate
// see Supported Pixel Formats in the Quartz 2D Programming Guide
// Creating a Bitmap Graphics Context section
// only RGB 8 bit images with alpha of kCGImageAlphaNoneSkipFirst, kCGImageAlphaNoneSkipLast, kCGImageAlphaPremultipliedFirst,
// and kCGImageAlphaPremultipliedLast, with a few other oddball image kinds are supported
// The images on input here are likely to be png or jpeg files
if (alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;
// Build a bitmap context that's the size of the thumbRect
CGContextRef bitmap = CGBitmapContextCreate(
NULL,
thumbRect.size.width, // width
thumbRect.size.height, // height
CGImageGetBitsPerComponent(imageRef), // really needs to always be 8
4 * thumbRect.size.width, // rowbytes
CGImageGetColorSpace(imageRef),
alphaInfo
);
// Draw into the context, this scales the image
CGContextDrawImage(bitmap, thumbRect, imageRef);
// Get an image from the context and a UIImage
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage* result = [UIImage imageWithCGImage:ref];
CGContextRelease(bitmap); // ok if NULL
CGImageRelease(ref);
return result;
}
It all depends on your file format... most photos are done in JPEG format, which is a fast fourier transform algorithm in the back end. The rule of thumb is that the more variations you have the image, the smaller dimension you'll have to use to achieve that 1MB threshold. The real solution is use an image library, ImageMagick been the easiest or libjpeg, make the file, check the size and then resize as necessary. There's no real easy answer if you're looking specifically to contain it under 1MB whereas it would be much easier for you to specify quality. But here are some tools that can help you out if you are developing the application:
for C++: Magick++ for image library, create the file and check the byte you're reading at... it may help
for Java: use JMagick for image library (the Java API for imagemagick), either make a bufferedbytearraystream (I think that's the name) and check the size after writing a jpeg to it or do the same with a file after creating a temporary file.
It's quite difficult to resize an image's dimensions so that the size of the file it would be encoded and saved into would be under a specific limit because it's nontrivial to predict how image encoding formats work on a specific image.
ImageMagick's command-line tools are able to restrict a file's size when you use JPEG encoding (with the "-define jpeg:extent=1mb
" switch) by degrading the quality of the image. I just took a look at the source (coders/jpeg.c
) and it basically just performs a binary search where in each loop iteration it encodes the image, checks the size and continues accordingly until it finds the maximum quality (i.e. compression level) setting that gives you a file under the specified size. Even if you'd like to reduce the image's dimensions instead of its compression level the idea and the algorithm are still the same.
You could do something similar using Apple's APIs, but if they don't seem adequate, ImageMagick can be compiled to work on the iPhone.
Note that doing something like this with large images and on a low-powered device like the iPhone will be very CPU-intensive.