Any code / library to reduce UIImage?

Is there any code or library that can help me reduce the image? If you take a picture with an iPhone, it is something like 2000x1000 pixels, which is not very network friendly. I want to scale it to say 480x320. Any clues?

+4
iphone graphics
Oct. 15 '09 at 17:07
source share
4 answers

This is what I use. Works good. I will definitely watch this question to see if anyone has the best / fastest. I just added the category below to UIimage .

 + (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize { UIGraphicsBeginImageContext( newSize ); [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)]; UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 
+8
Oct. 15 '09 at 17:14
source share

See http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/ - it has a set of code that you can download, as well as some descriptions.

If speed bothers, you can experiment using CGContextSetInterpolationQuality to set lower interpolation quality than default value.

+2
May 05 '10 at 11:45
source share

Please note: this is NOT my code. I ruined a little and found here . I decided that you would have to go into the CoreGraphics level, but I'm not quite sure about the specifics. That should work. Just be careful in managing your memory.

 // ============================================================== // resizedImage // ============================================================== // Return a scaled down copy of the image. UIImage* resizedImage(UIImage *inImage, CGRect thumbRect) { CGImageRef imageRef = [inImage CGImage]; CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef); // There 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 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; } 
0
Oct. 15 '09 at 17:14
source share

See the solution I posted to this question . The question involves rotating the image 90 degrees instead of scaling, but the premise is the same (it's just a matrix transformation that is different).

0
Oct. 15 '09 at 17:18
source share



All Articles