Quote:
Originally Posted by ralmeida
IT WORKS
All above code with minimal tweaks:
Code:
...
#include <math.h>
static inline double radians (double degrees) {return degrees * M_PI/180;}
...
-(UIImage *)resizeImage:(UIImage *)image :(NSInteger) width :(NSInteger) height {
CGImageRef imageRef = [image CGImage];
CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef);
CGColorSpaceRef colorSpaceInfo = CGColorSpaceCreateDeviceRGB();
if (alphaInfo == kCGImageAlphaNone)
alphaInfo = kCGImageAlphaNoneSkipLast;
CGContextRef bitmap;
if (image.imageOrientation == UIImageOrientationUp || image.imageOrientation == UIImageOrientationDown) {
bitmap = CGBitmapContextCreate(NULL, width, height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo);
} else {
bitmap = CGBitmapContextCreate(NULL, height, width, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, alphaInfo);
}
if (image.imageOrientation == UIImageOrientationLeft) {
NSLog(@"image orientation left");
CGContextRotateCTM (bitmap, radians(90));
CGContextTranslateCTM (bitmap, 0, -height);
} else if (image.imageOrientation == UIImageOrientationRight) {
NSLog(@"image orientation right");
CGContextRotateCTM (bitmap, radians(-90));
CGContextTranslateCTM (bitmap, -width, 0);
} else if (image.imageOrientation == UIImageOrientationUp) {
NSLog(@"image orientation up");
} else if (image.imageOrientation == UIImageOrientationDown) {
NSLog(@"image orientation down");
CGContextTranslateCTM (bitmap, width,height);
CGContextRotateCTM (bitmap, radians(-180.));
}
CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), imageRef);
CGImageRef ref = CGBitmapContextCreateImage(bitmap);
UIImage *result = [UIImage imageWithCGImage:ref];
CGColorSpaceRelease(colorSpaceInfo);
CGContextRelease(bitmap);
CGImageRelease(ref);
return result;
}
|
Hi,
thanks a lot for the info.
i've used the code and it does what it's expected but i do encounter some memory problems after using it.
in my app, the user will select a photo form the photo lib, after selecting it, the application will display the photo in a UIImageView and later will be taken form the imageView for some processing.
I'm a bit new to iphoen programming and most chances i'm doing something wrong.
Since i want to fix the rotation problem, i use the method inorder ot resize the photo to the original size (by that i just fix the rotatin problem).
I call the routine with the selected image size from the DidFinishPickingMediaWithInfo method (is it ok to call it from there ?)
and after one or two selections i start to get memory warnings.
what i do in DidFinishPickingMediaWithInfo is basicly :
(assuming img is the original selected image
[myImageView setImage:[self resizeImage:img height:img.size.height width:img.size.width];
after that, as i said ... memory problem.
Am i missing a way to only change the size for displaying the photo but using it later in its original size ?
thanks a lot,
Kobi.