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

时间:2011-05-12 03:49:49

标签: iphone image

我正在横向和纵向模式上传从iphone点击的图像。 具有横向模式的图像上传得很好,但问题在于以纵向模式上传的图像。它们旋转了90度。

其他具有纵向模式的图像(不是从iPhone点击)也能正常工作。

知道为什么会这样吗?

3 个答案:

答案 0 :(得分:5)

在你的代表中:

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

从密码“UIImage”获取“info”字典的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}字典中获取“UIImage”键的UIImagePickerControllerEditedImage

祝你好运。

答案 1 :(得分:4)

我已经解决了这个问题,我正在开展一个项目,我需要实际旋转图像,就像重新排列像素一样,我可以上传它。

您需要做的第一件事是确定方向,然后剥离那些讨厌的元数据,然后旋转图像。

所以把它放在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;
    }
}

这是resize函数:

+(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变量现在包含正确旋转的图像。

答案 2 :(得分:0)

好的,更清洁的版本是:

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

}
相关问题