在肖像模式下从iPhone点击的图像旋转90度

问题描述:

我上传的图像从iphone点击从横向和纵向模式。 横向模式的图像上传正常,但问题是与肖像模式上传的图像。他们旋转了90度。在肖像模式下从iPhone点击的图像旋转90度

其他图片与肖像模式(不从iPhone单击)工作正常。

任何想法为什么会发生这种情况?

+0

你在哪里上传到? – shabbirv 2011-05-12 04:01:09

+0

我将它上传到我的服务器 – 2011-05-12 04:56:44

+0

实际上图像已经旋转,但它在iPhone和Mac上正常显示。 – 2011-05-16 09:19:06

在您的委托:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 

后你会得到你的UIImage从“信息”字典键“UIImagePickerControllerOriginalImage”,就可以看到imageOrientation财产的形象定位。如果不像你想在上传之前旋转你的图片。

imageOrientation 
The orientation of the receiver’s image. (read-only) 

@property(nonatomic, readonly) UIImageOrientation imageOrientation 
Discussion 
Image orientation affects the way the image data is displayed when drawn. By default, images are displayed in the “up” orientation. If the image has associated metadata (such as EXIF information), however, this property contains the orientation indicated by that metadata. For a list of possible values for this property, see “UIImageOrientation.” 

Availability 
Available in iOS 2.0 and later. 
Declared In 
UIImage.h 

UIImage Class Reference

UIImagePickerController Class Reference

UIImagePickerControllerDelegate Protocol Reference

第二个选项:

是允许用户编辑您的图像和获取图像 “UIImagePickerControllerEditedImage”;

将您的UIImagePickerControllerallowsEditing”属性设置为Yes

在您的代表中,您只需从“info”字典中获取“UIImagePickerControllerEditedImage”密钥的UIImage即可。

祝你好运。

+0

这正是我想到的。但我的问题是,当iPhone上没有点击上传的图片时,一切正常。但只有当它通过iPhone点击后,它才会旋转。那么与iPhone方向的问题是,当它将图像转换为nsdate时,它将以旋转的形式提供数据? – 2011-05-12 11:17:51

+0

获取图像方向的uiimage测试后,如果方向有误,并将其转换为nsdata,则将其旋转。 – 2011-05-12 11:56:43

+1

问题是当我上传一个正常的肖像图像,然后它工作正常,但是当我上传从iPhone捕获的肖像图像,然后只有它被旋转。另外如果我在Windows机器上看到这个图像,它已经旋转了,但是当我在Mac或iPhone上看到相同的图像时,它看起来很好。 – 2011-05-12 13:52:04

我已经与这个问题摔跤了很久,我正在研究一个需要实际旋转图像的项目,就像重新排列像素以便我可以上传它一样。

您需要做的第一件事是确定方向,然后去掉那些烦人的元数据,然后旋转图像。

于是就把这里面的didFinishPickingMediaWithInfo功能:

UIImage * img = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; 

    if ([info objectForKey:@"UIImagePickerControllerMediaMetadata"]) { 
    //Rotate based on orientation 
    switch ([[[info objectForKey:@"UIImagePickerControllerMediaMetadata"] objectForKey:@"Orientation"] intValue]) { 
     case 3: 
      //Rotate image to the left twice. 
      img = [UIImage imageWithCGImage:[img CGImage]]; //Strip off that pesky meta data! 
      img = [rotateImage rotateImage:[rotateImage rotateImage:img withRotationType:rotateLeft] withRotationType:rotateLeft]; 
      break; 

     case 6: 
      img = [UIImage imageWithCGImage:[img CGImage]]; 
      img = [rotateImage rotateImage:img withRotationType:rotateRight]; 
      break; 

     case 8: 
      img = [UIImage imageWithCGImage:[img CGImage]]; 
      img = [rotateImage rotateImage:img withRotationType:rotateLeft]; 
      break; 

     default: 
      break; 
    } 
} 

这里是调整大小功能:

+(UIImage*)rotateImage:(UIImage*)image withRotationType:(rotationType)rotation{ 
    CGImageRef imageRef = [image CGImage]; 
    CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef); 
    CGColorSpaceRef colorSpaceInfo = CGColorSpaceCreateDeviceRGB(); 

    if (alphaInfo == kCGImageAlphaNone) 
     alphaInfo = kCGImageAlphaNoneSkipLast; 

     CGContextRef bitmap; 

    bitmap = CGBitmapContextCreate(NULL, image.size.height, image.size.width, CGImageGetBitsPerComponent(imageRef), 4 * image.size.height/*CGImageGetBytesPerRow(imageRef)*/, colorSpaceInfo, alphaInfo); 
    CGColorSpaceRelease(colorSpaceInfo); 

    if (rotation == rotateLeft) { 
     CGContextTranslateCTM (bitmap, image.size.height, 0); 
     CGContextRotateCTM (bitmap, radians(90)); 
    } 
    else{ 
     CGContextTranslateCTM (bitmap, 0, image.size.width); 
     CGContextRotateCTM (bitmap, radians(-90)); 
    } 

    CGContextDrawImage(bitmap, CGRectMake(0, 0, image.size.width, image.size.height), imageRef); 
    CGImageRef ref = CGBitmapContextCreateImage(bitmap); 
    UIImage *result = [UIImage imageWithCGImage:ref]; 
    CGImageRelease(ref); 
    CGContextRelease(bitmap); 
    return result; 
} 

img变量现在包含一个正确旋转图像。

好,更清洁的版本是:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 
    UIImage *img = [info valueForKey:UIImagePickerControllerOriginalImage]; 
    img = [UIImage imageWithCGImage:[img CGImage]]; 

    UIImageOrientation requiredOrientation = UIImageOrientationUp; 
    switch ([[[info objectForKey:@"UIImagePickerControllerMediaMetadata"] objectForKey:@"Orientation"] intValue]) 
    { 
     case 3: 
      requiredOrientation = UIImageOrientationDown; 
      break; 
     case 6: 
      requiredOrientation = UIImageOrientationRight; 
      break; 
     case 8: 
      requiredOrientation = UIImageOrientationLeft; 
      break; 
     default: 
      break; 
    } 

    UIImage *portraitImage = [[UIImage alloc] initWithCGImage:img.CGImage scale:1.0 orientation:requiredOrientation]; 

}