iPhone taking a picture when saving always landscape - iphone

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.

Related

how to convert landscape image to portrate image in IOS6?

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

Using launch image as ViewController background image

I created my 3 launch images (Default Default#2x Default-568#2x) at their appropriate resolutions. I'd like to use these images as the background image of my initial View Controller.
However, when I designed my launch images, I did it with the statusbar in mind and left that area blank. When I try to set the background image of the View Controller to the launch image using the following code, my launch image begins at the bottom of my status bar and the blank area is visible. Essentially, my 640x1136 launch image is being squeezed into a 640x1096 space. What is the proper way to do what I'm attempting?
UIImage *backgroundImage = [[UIImage alloc]init];
if ([[UIScreen mainScreen] bounds].size.height == 568) {
backgroundImage = [UIImage imageNamed:#"Default-568h#2x"];
}
else{
backgroundImage = [UIImage imageNamed:#"Default"];
}
UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:backgroundImage];
[backgroundImageView setFrame:[[self view] bounds]];
[[self view] addSubview:backgroundImageView];
Update:
I replaced:
[backgroundImageView setFrame:[[self view] bounds]];
With:
[backgroundImageView setFrame:CGRectMake(0 ,-20,self.view.frame.size.width , self.view.frame.size.height+20)];
And it seems to be behaving the way I want now.
Try this:
[self.view setBackgroundColor:[UIColor colorWithPatternImage:backgroundImage]];
Also try not setting backgroundImageView's frame programatically. It should already be the size it should be when you call initWithImage:.
Make use of unedited image which does not contain the blank area. this will work fine.
Check replacing this [backgroundImageView setFrame:[[self view] bounds]]; with the following code.
int statusBarHeight = statusBar.frame.size.height;
backgroundImageView.frame = CGRectMake(0 ,statusBarHeight,self.view.frame.size.width , self.view.frame.size.height-statusBarHeight);

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

crop image in circular shape

I am creating an application in which i am displaying one .jpg image. I want to crop part of image in circular shape. Please help me to solve this problem.
image = [UIImage imageNamed:#"images2.jpg"];
imageView = [[UIImageView alloc] initWithImage:image];
CGSize size = [image size];
[imageView setFrame:CGRectMake(0, 0, size.width, size.height)];
[[self view] addSubview:imageView];
[imageView release];
Please tell me to crop part of image in circular shape
You can do it by using Quartz Core framework it really has some cool Apis to do that. Check this RoundedImageView example.
Methinks this is a duplicate. There's an excellent accepted answer in this question, and links to other articles: How to crop UIImage on oval shape or circle shape?
EDIT: There's a handful of easy ways of going about this. A CALayer with a cornerRadius being obvious. But more importantly, there exists the method CGImageCreateWithMask: which can be applied to a broader spectrum of up to and including circles and other shapes. Note that if your image is a JPEG, then CGImageCreateWithMask will return a black background because JPEG's have no alpha channel.
You can use RSKImageCropper for crop the image in circular shape. I am implemented the fallowing code to crop the image in circular shape with the help of RSKImageCropper.
1. Install the pod RSKImageCropper.
2. #import <RSKImageCropper/RSKImageCropper.h> in your viewcontroller
3. Add delegate to your interface i.e. RSKImageCropViewControllerDelegate
4. Implement the fallowing code in **didFinishPickingMediaWithInfo** delegate.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:
^{
RSKImageCropViewController *imageCropVC = [[RSKImageCropViewController alloc] initWithImage:originalImage];
imageCropVC.avoidEmptySpaceAroundImage = YES;
imageCropVC.delegate = self;
[self presentViewController:imageCropVC animated:NO completion:nil];
}];
}
5. Now implement the delegate of RSKImageCropper.
- (void)imageCropViewControllerDidCancelCrop:(RSKImageCropViewController *)controller
{
[controller dismissViewControllerAnimated:NO completion:nil];
}
// The original image has been cropped.
- (void)imageCropViewController:(RSKImageCropViewController *)controller
didCropImage:(UIImage *)croppedImage
usingCropRect:(CGRect)cropRect
{
self.imgVIew.image = croppedImage;
[self.navigationController popViewControllerAnimated:YES];
}
// The original image has been cropped. Additionally provides a rotation angle used to produce image.
- (void)imageCropViewController:(RSKImageCropViewController *)controller
didCropImage:(UIImage *)croppedImage
usingCropRect:(CGRect)cropRect
rotationAngle:(CGFloat)rotationAngle
{
self.imgVIew.image = croppedImage;
[controller dismissViewControllerAnimated:NO completion:nil];
}
For more info check this https://github.com/ruslanskorb/RSKImageCropper

uiimageview aspectfit question

I've use a imagepicker to select image from my photo library, then i display that image in an uiimageview. Landscape photo works fine but there is some weirdness to the portrait image. The portrait image suppose to fill up the left and right empty space but it's not.
Cant figure out why the picture wont fill up the left and right space cause the imageview frame did specify the mainScreen bounds.
If i take away the aspectfit then the potrait image is nicely display but the landscape image is stretched to fill up the whole imageview.
My code is as follow:
CGRect frame = [[UIScreen mainScreen] bounds];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:frame];
UIImageView *selectedImageView = [[UIImageView alloc] initWithFrame:frame];
selectedImageView.image = image;
selectedImageView.contentMode = UIViewContentModeScaleAspectFit;
[scrollView addSubview:selectedImageView];
[selectedImageView release];
scrollView.delegate = self;
[self.view addSubview:scrollView];
[scrollView release];
Here is what it looks like:
Edit: My goal is to display the photo like it's been display in Photos album, start out as fitting in the view and allow zoom in to a certain limit.
Try out
UIImageView *logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"logo_bg.png"]];
[logoImageView setFrame:CGRectMake(0, 0, 320, [UIImage imageNamed:#"logo_bg.png"].size.height)];
[mainView addSubview:logoImageView];