Hi guys,
I'm actually trying to take an image with pickerimagecontroller and then make it in greyscale.
All the image selection/camera use process work fine and I have my UIImage displayed in a UIImageView and placed in a View with Interface Builder. All is fine.
Then I've added some code to convert the image to grayscale and it work, but it make me crazy beacause it rotate and stretch the image only in certain cases...
So I've found this code surfing the web:
Code:
- (UIImage *) grayscaleImage: (UIImage *) image
{
CGSize size = image.size;
//CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
NSLog([NSString stringWithFormat:@"width: %g",image.size.width]);
NSLog([NSString stringWithFormat:@"height: %g",image.size.height]);
// Create a mono/gray color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef context = CGBitmapContextCreate(nil, size.width,
size.height, 8, 0, colorSpace, kCGImageAlphaNone);
//CGContextTranslateCTM(context, image.size.width, 0);
CGColorSpaceRelease(colorSpace);
// Draw the image into the grayscale context
CGContextDrawImage(context, rect, [image CGImage]);
CGImageRef grayscale = CGBitmapContextCreateImage(context);
CGContextRelease(context);
// Recover the image
UIImage *img = [UIImage imageWithCGImage:grayscale];
CFRelease(grayscale);
return img;
}
Then I've used it directly here:
Code:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
foto.image = [ self grayscaleImage: [info objectForKey:@"UIImagePickerControllerOriginalImage"]];
}
So...
it work and the image is in grayscale but it has a really strange rotation/stratching bug that I can't explain myself.
1) If I take a photo with 3gs 1536x2048 VERTICAL (a normal portrait photo),
instead of displaying the photo with originals proportions and vertical oriented, all I get is the image ROTATED About 90° and streched (height and widht inverted)
2) The strange thing is if I choose an image 320x480 (not taken with camera but saved from an mail) it will display it CORRECTLY with the ORIGINAL ALIGNMENT and PROPORTION and also in greyscale...so perfectly
So why??
Why 320x480 saved from email work and a photo made with iphone camera don't work??
Can anyone please can help me to find a solution?
Thanks!
:-D