Converting UIImage to CGImage cause rotation of Image - iphone

I am trying to crop image by my custom cropper. Not from the UIImagePickerController's. Now for that I am showing the Image captured from Image Picker in a UIImgeView by handling UIImagePicker's delegate:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
imvOriginalImage.image = image;
}
For cropping I have to convert this Image to CGImageRef. The Image is captured in portrait mode. Now when I convert this image in CGImageRef image orientation get changed. Even if I use following:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
//imvOriginalImage.image = image;
CGImageRef imgRef = [image CGImage];
UIImage *img = [UIImage imageWithCGImage:imgRef];
[imvOriginalImage setImage:img];
}
Image Orientation get changed. This happens only for those Image which are captured in portrait mode. Landscape images not have this issue.
Why this is happening?
How to solve this?
Thanks In Advance.

This is that iPhone camera rotates the image comparing with Home button. It uses EXIF that is meta data of image. See this thread:
iPhone image rotation

Related

UIImageOrientationRight(left) won't work

I want to pick a photo from UIImagePickerController and check the photo if it is landscape. if selected photo is landscape, I want to rotate to portrait.
so, here is my code
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo {
if(image.imageOrientation == UIImageOrientationRight || image.imageOrientation == UIImageOrientationLeft){
UIImage *retatedImg = [image imageRotatedByDegrees:90];
}else {
UIImage *retatedImg = image;
}
}
I'm sure [imageRotatedByDegrees:] method is working fine. Just stuck why it does not recognize landscape photo. help me!
imageOrientation property doesn't refer to the format of the image. When you load an image, the value for that property depends of the EXIF data in the file, if any. If the camera that take the photo saves it with the final format, it will not include EXIF data for image orientation changes.
If you want to know if that image format is portrait or landscape, compare the width and height dimensions instead of using imageOrientation.
if( image.size.width > image.size.height )
UIImage *retatedImg = [image imageRotatedByDegrees:90] ;

Save zoomed image from camera

I am developing a camera application for iphone/ipad.
We are using an overlay for displaying the camera app on top of the viewfinder.
Currently i am trying to save the zoomed image. We are able to zoom the image on viewfinder. But when we save the image it gets saved in the original size.
To solve this we are scaling the zoomed image using the following code :
UIImageView *v = [[UIImageView alloc]initWithImage:image];
UIGraphicsBeginImageContext(v.bounds.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextScaleCTM(context, zoomvalue,zoomvalue);
[v drawRect:CGRectMake(0,0,320,480)];
CGContextRestoreGState(context);
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Using the above code we are able to get the zoomed image.
However, we need to crop the center portion of the scaled image and save the same. We are not getting the same.
Kindly help
if You are using UIImagePickerController to capture image and zoom your image there then you can get the edited image by getting UIImagePickerControllerEditedImage from picker NSDictionary something like this -
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
img = [[info objectForKey:UIImagePickerControllerEditedImage] retain];
[picker dismissModalViewControllerAnimated:YES];
}

UIImagePNGRepresentation ..... writeToFile is always landscape

Each time I use the camera to take a photograph, then save it the image is always in landscape. This means the UIImageView in my Xib is the wrong way around. Its Portrait - which is what I want and expected. I can correct this by rotating the image 90 degrees but even then I can't disable the animation which shows the original landscape photo followed by the animated rotation itself. How can I ensure the UIImageView from the camera is actually in portrait mode when saved and not landscape and, how can I disable the animation during the CGAffineTranformationMakeRotation ??
I found this post to be very helpful, it describes adding a category to UIImage to do the rotation of the image before you save it:
http://www.platinumball.net/blog/2009/03/30/iphone-uiimage-rotation-and-mirroring/
And then once the category is implemented, you would do something like this:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *originalImage, *editedImage, *rotatedImage;
editedImage = (UIImage *)[info objectForKey:UIImagePickerControllerEditedImage];
originalImage = (UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage];
if (editedImage)
{
rotatedImage = [editedImage rotate:UIImageOrientationRight];
}
else
{
rotatedImage = [originalImage rotate:UIImageOrientationRight];
}
NSString *f = #"/set/this/to/your/file/name";
[UIImagePNGRepresentation(rotatedImage) writeToFile:f atomically:YES];
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
[picker release];
}

Resolution of image from photo library is always 640 * 480

I am working on one module where I need to pick image from photo library and draw on view.but whenever I pick the large scale images it always return me 640 *480 scaled image and because of that small image is displayed.
I have made AllowEditing ON.
can anyone help me to find the resolution of original image,so that I can again scale it to original one.
iImagePicker.allowsImageEditing = YES;
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
[[iImagePicker parentViewController] dismissModalViewControllerAnimated:YES];
iIsImageSaved = YES;
iSavedImage = [editingInfo objectForKey:#"UIImagePickerControllerOriginalImage"];;
int width,height;
width = iSavedImage.size.width;
height = iSavedImage.size.height;
iApp->ImagePicked(image);
}
Thanks,
Sagar
You don't need to scale your image back, it should be available anyway. Check my answer to this question (which is a duplicate I think..)
[Updated answer for your code]
You are using a deprecated method, try imagePickerController:didFinishPickingMediaWithInfo: with the UIImagePickerControllerOriginalImage key instead.
Added code snippet:
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage* originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSLog(#"Original image width: %f and height: %f", originalImage.size.width, originalImage.size.height);
UIImage* editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
NSLog(#"Edited image width: %f and height: %f", editedImage.size.width, editedImage.size.height);
[self dismissModalViewControllerAnimated:YES];
}

incorrect values for UIImagePickerControllerCropRect rectangle

I am working on an application where i am picking up images from iphone library using uiimagepicker control. I am using following code to pick up image.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(#"image picked:");
UIImage *image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
CGRect cropRect;
cropRect = [[info valueForKey:#"UIImagePickerControllerCropRect"] CGRectValue];
}
The above method works perfect when I capture an image from camera but when I pick an image from iphone library, the cropRect gives me incorrect values. It is always set to x = 43 or greater, even if I pick the rectangle from extreme left of the screen. So as a result I am getting a vertical black strip on left side of the image.
thanks in advance