how to convert landscape image to portrate image in IOS6? - iphone

I am trying to take picture from camera in landscape mode and I want to show that image in portrait mode in iOS6. I can't rotate the image in portrait mode.

You want to rotate, then what you are gonna to do with rotated image?
If you want to just show it in view, wrap it in UIImageView, rotate imageView and insert into view:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage* image = [[info objectForKey: UIImagePickerControllerOriginalImage] fixOrientation];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.transform = CGAffineTransformMakeRotation(M_PI * 90 / 180);
[self.view addSubview:imageView];
}

Related

UIProgressView when putting image into UIImageView

I wonder, how could I display UIProgressView in this case:
I do photo with UIImagePickerController then in method - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
I save the picture to UIImage object (outputImage) and hide controller, and when modalView/controller with camera is disappeared I have such code:
NSLog(#"Image is width: %f height: %f", outputImage.size.width, outputImage.size.height);
//FLIP PICTURE FROM FACETIME CAMERA
if((outputImage.size.width == 480 && outputImage.size.height == 640) || (outputImage.size.width == 640 && outputImage.size.height == 480)) {
outputImage = [UIImage imageWithCGImage:outputImage.CGImage
scale:1.0 orientation: UIImageOrientationLeftMirrored];}
//RESIZE TO 1000x1000
outputImage = [self scaleAndCropToSize:outputImage toSize:CGSizeMake(1000, 1000)];
NSLog(#"Now image is width: %f height: %f", outputImage.size.width, outputImage.size.height);
//CUTING OUT TO MASK
outputImage = [self maskImage:outputImage withMask:[UIImage imageNamed:#"mask.png"]];
//ADDING SHADOW
CGSize size = CGSizeMake(1000, 1000);
UIGraphicsBeginImageContext(size);
CGPoint thumbPoint = CGPointMake(0, 0);
[outputImage drawAtPoint:thumbPoint];
UIImage* shadow = [UIImage imageNamed:#"shadow.png"];
CGPoint starredPoint = CGPointMake(0, 0);
[shadow drawAtPoint:starredPoint];
UIImage* result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
outputImage = result;
//ADDING TO UIIMAGEVIEW (PREVIEW)
preview.image = outputImage;
While photo is being put into UIImageView, it takes some time. In this time I'd like to display progress view. How can I do that?
EDIT: I thought to place above code in some method and then somehow implement progressview, that would base on method completion time. But dunno.
You can not use UIProgressView as its difficult to know how much time it will take to perform image operations.
you can try this MBProgressHUD
Put this code in didFinishPickingMediaWithInfo
MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view];
HUD.labelText = #"Saving Image...";
[self.view addSubview:HUD];
HUD.delegate = self;
[HUD showWhileExecuting:#selector(savingPicture:) onTarget:self withObject:selectedImage animated:YES];
and put your resize/crop image operations code in savingPicture method.
Progress views are more appropriate when you are running a loop and you know what percentage of the work is being completed, to show the progress in the progress bar. In this case you can show a UIActivityIndicator instead, until your image is ready to be put on the image view.

How to get rid of the black ground on the image returned from UIImagePickerControllerEditedImage when using UIImagePicker

When using UIImagePicker, it allows to get into Edit mode. Then for an image taken with landscape mode, if user don't scale it manually within the UIImagePicker, there would have black stripe/background on the top and bottom of the image. Is there a way to get rid of that and make that portion of the image to be transparent?
#pragma mark UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *img = nil;
CGRect cropRect = [[info valueForKey:UIImagePickerControllerCropRect] CGRectValue];
if (cropRect.origin.y < 0) {
img = [info objectForKey:UIImagePickerControllerOriginalImage];
} else {
img = [info objectForKey:UIImagePickerControllerEditedImage];
}
}

UIGraphicsGetImageFromCurrentImageContext rotate the image at will?

In brief, the app is meant to do the following:
The user will select from the photos within the iPhone (via ALAssets). This part is fine.
The selected photo will appear on another view, within a smaller, rectangular subview.
When using the ALAsset thumbnail, the image shows up correctly within the subview. However, the resolution is poor, so I'm trying to use a higher resolution image.
When using the full resolution image, and then placing it into the view, any portrait photo will be rotated 90 degrees counter-clockwise.
Code-wise, this it looks like this:
//ImageView is the UIImageView subview that will hold the selected photo image.
//This code works ok, but I'd rather have a higher resolution photo.
[imageView setClipsToBounds:YES];
[imageView setContentMode:UIViewContentModeScaleAspectFill];
[imageView setImage:[UIImage imageWithCGImage:[selectedPhoto thumbnail]];
Now, this is the code where I try to use a higher resolution image, but then the image within imageView is rotated -90 degrees:
[imageView setClipsToBounds:YES];
[imageView setContentMode:UIViewContentModeScaleAspectFill];
//HIGH RESOLUTION IMAGE
ALAssetRepresentation *rep = [selectedPhoto defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
if (iref)
{
UIImage *largeImage = [Utilities imageWithImage:[UIImage imageWithCGImage:iref] scaledToSize:CGSizeMake(CGImageGetWidth(iref)/4, CGImageGetHeight(iref)/4)];
[imageView setImage:largeImage];
}
And the imageWithImage: function called there looks like this:
+ (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
As mentioned, photos taken in landscape orientation by the iPhone camera come out OK. However, photos taken in portrait camera orientation come out rotated -90 degrees somehow.
How can I make it so that each photo, whether taken in portrait or landscape, will come out oriented correctly on my ImageView?
Any help would be appreciated! Thank you!
I found the answer here!
ALAssetRepresentation fullResolutionImage's UIImageOrientation is wrong
I am doing this now:
UIImage *largeImage = [[UIImage alloc] initWithCGImage:iref scale:4 orientation:(UIImageOrientation)rep.orientation];

iPhone : Image Captured in portrait mode is loaded in landscape mode in UIImageview

I am developing an Application where I am capturing image in portrait mode and then moving to another class with Image data and showing that image in a UIImageView on a UIScrollView.
Now If I am taking image form library,it is working fine, but If I have to capture an Image it is showing data in landscape mode. Below is my code of both classes. :
Class 1:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSData *imgData = UIImagePNGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"]);
[picker dismissModalViewControllerAnimated:YES];
image = [[UIImage alloc] initWithData:imgData];
CGImageRef imgRefCrop = image.CGImage;
UIImage *photo = [UIImage imageWithCGImage:imgRefCrop];
PhotoCropperPage *photoCropper = [[PhotoCropperPage alloc]
initWithPhoto:photo
delegate:self
uiMode:PCPUIModePresentedAsModalViewController
showsInfoButton:YES];
[photoCropper setMinZoomScale:0.3f];
[photoCropper setMaxZoomScale:4.0f];
[self.navigationController pushViewController:photoCropper animated:YES];
}
Class 2:
- (void) loadPhoto
{
if (self.photo == nil) {
return;
}
CGFloat w = self.photo.size.width;
CGFloat h = self.photo.size.height;
CGRect imageViewFrame = CGRectMake(0.0f, 0.0f, roundf(w / 2.0f), roundf(h / 2.0f));
self.scrollView.contentSize = imageViewFrame.size;
UIImageView *iv = [[UIImageView alloc] initWithFrame:imageViewFrame];
iv.image = self.photo;
[self.scrollView addSubview:iv];
self.imageView = iv;
[iv release];
}
I am not getting where I am doing a mistake? Can anybody point me out? Thanks in advance.
Just found an awesome answer to your question. Here's the link: https://stackoverflow.com/a/5427890/1047258
And then you can do something like that:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *theImage = [[info objectForKey:#"UIImagePickerControllerOriginalImage"]fixOrientation];
//rest of your code
}
and theImage will be your image with correct orientation. It worked in my project
Hope it helps.
be careful not to throw away the orientation data... Try this:
image = [[UIImage alloc] initWithData:imgData];
CGImageRef imgRefCrop = image.CGImage;
UIImage *photo = [UIImage imageWithCGImage:imgRefCrop scale:image.scale orientation:image.orientation ];
also, you'll want to make sure you're cropping the right pixels depending in the image's orientation.

iPhone taking a picture when saving always landscape

I have this iPhone application that let users take a picture and save it in a database online. My problem is that every time an user take a picture and saves it, the picture results to be in landscape, even though it was taken in portrait mode. This results in having the portrait picture stretched.
This is the code I use when taking a picture:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
NSData *imgData=UIImageJPEGRepresentation([info objectForKey:#"UIImagePickerControllerOriginalImage"],1);
UIImage *img=[[UIImage alloc] initWithData:imgData];
if(img.size.width < img.size.height){
NSLog(#"width < height");
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
}
else{
NSLog(#"width > height");
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 460, 320)];
}
imageView.image = img;
[img release];
[[self view] fillPreviewWithImg: imageView];
[[self view] setImage: imageView.image];
}
Basically what I do is take the picture, create a UIImage, check if it's portrait or landscape, create the corresponding UIImageView and then set the image into the UIImageView. After that I just call a couple of methods to set up the image in the main view.
I believe the problem with stretching is not bounded to the PHP but to the Objective-C code, but I can't really see how or why this behavior happens.
Does anyone of you have an idea?
Thanks,
Masiar
Have a look at the imageOrientation property of the UIImage class. You can have an image that is 320 wide and 480 high, but with an orientation of Landscape. This is contained in the EXIF information and it is up to the viewing program to use this orientation information to rotate the image appropriately. Just checking the width and height of the image is not sufficient to know the orientation and this is causing your stretching that you are seeing.