Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 01-12-2010, 07:15 AM   #1 (permalink)
Dvdkite
Registered Member
 
Join Date: Sep 2009
Posts: 96
Default UIImage GrayScale convertion OK but image Rotate/Strecth!!! Why???

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
Dvdkite is offline   Reply With Quote
 

» Advertisements
» Stats
Members: 158,870
Threads: 89,225
Posts: 380,694
Top Poster: BrianSlick (7,129)
Welcome to our newest member, triclopus
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:44 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.