Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-12-2010, 06:15 AM   #1 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 64
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
Old 01-12-2010, 08:40 AM   #2 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 64
Default

Hi guys,

The way of mistery is open...

I've processed the UIImage taken from the picker with this method BEFORE apply the GreyScale (above):

Code:
- (UIImage*)resizeImage:(UIImage*)image 
{	
	CGSize size = image.size; 
	UIGraphicsBeginImageContext( size );
	[image drawInRect:CGRectMake(0,0,size.width ,size.height )];
	UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	
	return newImage;
}
:

As you can see there's no resize...scale factor is one...

Now all works and it's perfect...
maybe this code will be useful for someone with the same problem...

I would like to know why I should do this trick to make it work correctly?
Why I must scale an UIImage obtined from the picker controller in order to have the CORRECT alignment and proportion????

any guru could explane this?

Thank you!!
Dvdkite is offline   Reply With Quote
Reply

Bookmarks

Tags
alignment, grayscale, uiimage

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 24,198
Threads: 38,970
Posts: 170,966
Top Poster: smasher (2,567)
Welcome to our newest member, Hendrika
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:39 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0