UIImageOrientationRight(left) won't work - iphone

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] ;

Related

Issues taking pictures with ZBarReader in landscape orientation and flat iPhone position

I'm using ZBar to detect codes but also, I want to enable taking pictures from the same screen. I detected an odd behaviour taking pictures in landscape orientation. If I put my mobile in vertical landscape position the image comes out ok, but If I move my iPhone to the flat landscape position, the picture comes out upside down. I checked UIImage metadata and the image orientation has different values, despite the fact that the device orientation is the same in both cases.
Any idea why this happens?
My solution is to change the image orientation metadata in the wrong cases:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[....]
UIImage *image = [info objectForKey: UIImagePickerControllerOriginalImage];
if(image){
// This fixes a bug in ZBarReader taking picture in landscape orientation and device in flat position.
NSLog(#"Image: %d, Device: %d",image.imageOrientation,self.interfaceOrientation);
UIImageOrientation imgOrientation = image.imageOrientation;
UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft && imgOrientation == UIImageOrientationUp){
image = [UIImage imageWithCGImage:[image CGImage] scale:1.0 orientation:UIImageOrientationDown];
}else if(interfaceOrientation == UIInterfaceOrientationLandscapeRight && imgOrientation == UIImageOrientationDown){
image = [UIImage imageWithCGImage:[image CGImage] scale:1.0 orientation:UIImageOrientationUp];
}
[self hideScanner];
[self performSegueWithIdentifier:#"mySegue" sender:image];
}
}
}
}

UIImagePickerController check if user edited the image

I am using a UIImagePickerController with the property allowsEditing set to YES.
When the user finish picking an image I want to know if the user edited the image he selected or not (e.g. if he scaled the image). This method:
UIImage *editedImage = [info objectForKey:#"UIImagePickerControllerEditedImage"];
returns always an object even if the user left the picture as it was. Is there any way to check if the user edited the image? For example can i check if the UIImagePickerControllerEditedImage and UIImagePickerControllerOriginalImage are different somehow?
Try this in didFinishPickingMediaWithInfo as i am not sure:
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImage *editedimage = [info objectForKey:UIImagePickerControllerEditedImage];
if ([UIImagePNGRepresentation(image) isEqualToData:UIImagePNGRepresentation(editedimage)])
//not edited
else
//edited
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImage *editedimage = [info objectForKey:UIImagePickerControllerEditedImage];
if(editedimage.length>0){
//then got the edited image
}
Could you not just get and compare the CGSize of the image?
BOOL sizeChanged = FALSE;
// get current size of image
CGSize originalSize = [image size];
//After the user hase made the action, get the new size
CGSize currentSize = [image size];
// if the dimensions have been editied the condition is true
if ( originalSize.width != currentSize.width ||
originalSize.height != currentSize.height
)
sizeChanged = TRUE;
else
sizeChanged = FALSE;
Check this out:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImagePickerControllerDelegate_Protocol/UIImagePickerControllerDelegate/UIImagePickerControllerDelegate.html#//apple_ref/doc/uid/TP40007069
This is the docs for the ImagePicker Delegate. As you can see, when the user picks and image this is called:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
info - is a dictionary which contains data about what happened and what has been picked. if allowediting is set to YES then info contains both the original image and the edited one. Check in the link I gave you for the
Editing Information Keys
there are a bunch of constants there which can give you the data you seek!
Start from here to see the whole mechanics:
http://developer.apple.com/library/ios/documentation/uikit/reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html#//apple_ref/occ/instp/UIImagePickerController/allowsEditing
I know that this is a very old question, with no activity in awhile, but this is what comes up in a google search, and as far as I can tell, the question remains unanswered satisfactorily.
Anyway, the way to tell if the image has been edited or not is this:
In didFinishPickingMediaWithInfo: you can inspect the width of the CropRect and the width of the original image. If CropRect.width == originalImage.width+1, then it has not been edited. The reason this is true is because to edit the image, the user must pinch and zoom, which scales the image and changes the size of the CropRect. Simply moving the image around will not work as it bounces back unless it is scaled.
NSValue *pickerCropRect = info[UIImagePickerControllerCropRect];
CGRect theCropRect = pickerCropRect.CGRectValue;
UIImage *originalImage = info[UIImagePickerControllerOriginalImage];
CGSize originalImageSize = originalImage.size;
if (theCropRect.size.width == originalImageSize.width+1) {
NSLog(#"Image was NOT edited.");
} else {
NSLog(#"Image was edited.");
}
As far as I can tell this works in iOS 9 on the 6S and 6+. I see no real reason it shouldn't work elsewhere.

Converting UIImage to CGImage cause rotation of Image

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

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];
}

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