views:

375

answers:

1

Hi All,

I am taking pictures from the iPhone camera using the UIImagePickerController Class.

I am using this delegate method for getting the image.

  • (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

}

But when I am using that image in an ImageView or sending the image data to some url, the image is appearing rotated by 90 degree.

What is the issue ? Am i doing it correctly ?

Thanks

+1  A: 

You need to rotate the picture yourself depending on your orientation.

Use this Code (it may also resize your picture) I found it somewhere in the net, but cannot remember where:

@implementation UIImage (Resizing)

static inline double radians (double degrees) {return degrees * M_PI/180;}


- (UIImage*)imageByScalingToSize:(CGSize)targetSize {
 UIImage* sourceImage = self; 
 CGFloat targetWidth = targetSize.width;
 CGFloat targetHeight = targetSize.height;

 CGImageRef imageRef = [sourceImage CGImage];
 CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
 CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);

 if (bitmapInfo == kCGImageAlphaNone) {
     bitmapInfo = kCGImageAlphaNoneSkipLast;
 }

 CGContextRef bitmap;

 if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
     bitmap = CGBitmapContextCreate(NULL, targetWidth, targetHeight, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

 } else {
     bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

 }       

 if (sourceImage.imageOrientation == UIImageOrientationLeft) {
     CGContextRotateCTM (bitmap, radians(90));
     CGContextTranslateCTM (bitmap, 0, -targetHeight);

 } else if (sourceImage.imageOrientation == UIImageOrientationRight) {
     CGContextRotateCTM (bitmap, radians(-90));
     CGContextTranslateCTM (bitmap, -targetWidth, 0);

 } else if (sourceImage.imageOrientation == UIImageOrientationUp) {
     // NOTHING
 } else if (sourceImage.imageOrientation == UIImageOrientationDown) {
     CGContextTranslateCTM (bitmap, targetWidth, targetHeight);
     CGContextRotateCTM (bitmap, radians(-180.));
 }

 CGContextDrawImage(bitmap, CGRectMake(0, 0, targetWidth, targetHeight), imageRef);
 CGImageRef ref = CGBitmapContextCreateImage(bitmap);
 UIImage* newImage = [UIImage imageWithCGImage:ref];

 //CGContextRelease(bitmap);
 //CGImageRelease(ref);

 return newImage; 
}

@end
maxbareis