相机图像旋转问题

时间:2013-04-06 11:10:20

标签: ios objective-c uiimage uiimagepickercontroller exif

我在这里遇到一个非常奇怪的问题。当我在纵向模式下单击图像并上传它然后再次获取它时,显示旋转90度逆时针方向。但是当我在相机胶卷中看到它时,它会以正确的方向显示。我已经尝试了几乎所有可能的链接/代码来解决这个问题,但似乎没有任何帮助。我以JPEG格式保存图像。请帮助这个人。 在此先感谢!!

3 个答案:

答案 0 :(得分:10)

通过在UIImage上创建一个类别并根据元数据EXIF来缩放和旋转图像来解决。

这是一段神奇的代码:

- (UIImage *)scaleAndRotateImage:(UIImage *)image {

    int kMaxResolution = 640; // Or whatever

    CGImageRef imgRef = image.CGImage;

    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);


    CGAffineTransform transform = CGAffineTransformIdentity;
    CGRect bounds = CGRectMake(0, 0, width, height);
    if (width > kMaxResolution || height > kMaxResolution) {
        CGFloat ratio = width/height;
        if (ratio > 1) {
            bounds.size.width = kMaxResolution;
            bounds.size.height = roundf(bounds.size.width / ratio);
        }
        else {
            bounds.size.height = kMaxResolution;
            bounds.size.width = roundf(bounds.size.height * ratio);
        }
    }

    CGFloat scaleRatio = bounds.size.width / width;
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
    CGFloat boundHeight;
    UIImageOrientation orient = image.imageOrientation;
    switch(orient) {

        case UIImageOrientationUp: //EXIF = 1
            transform = CGAffineTransformIdentity;
            break;

        case UIImageOrientationUpMirrored: //EXIF = 2
            transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            break;

        case UIImageOrientationDown: //EXIF = 3
            transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;

        case UIImageOrientationDownMirrored: //EXIF = 4
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
            transform = CGAffineTransformScale(transform, 1.0, -1.0);
            break;

        case UIImageOrientationLeftMirrored: //EXIF = 5
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;

        case UIImageOrientationLeft: //EXIF = 6
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;

        case UIImageOrientationRightMirrored: //EXIF = 7
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeScale(-1.0, 1.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;

        case UIImageOrientationRight: //EXIF = 8
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;

        default:
            [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];

    }

    UIGraphicsBeginImageContext(bounds.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
        CGContextScaleCTM(context, -scaleRatio, scaleRatio);
        CGContextTranslateCTM(context, -height, 0);
    }
    else {
        CGContextScaleCTM(context, scaleRatio, -scaleRatio);
        CGContextTranslateCTM(context, 0, -height);
    }

    CGContextConcatCTM(context, transform);

    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return imageCopy;
}

答案 1 :(得分:3)

尝试使用这个:

NSData *profileData =  UIImageJPEGRepresentation(self.profileImgView.image, 1.0); 

而不是:

NSData *profileData = UIImagePNGRepresentation(profileImgView.image);

希望它有效。 :)

答案 2 :(得分:1)

我希望对你有所帮助:

在这里,我正在调整图像以及你的问题也将解决。

FirstWay:

    - (UIImage *)scaleAndRotateImage:(UIImage *)imgPic {

    int kMaxResolution = 650; // Or whatever    
    CGImageRef imgRef = imgPic.CGImage; 
    CGFloat width = CGImageGetWidth(imgRef);    
    CGFloat height = CGImageGetHeight(imgRef);  
    CGAffineTransform transform = CGAffineTransformIdentity;    
    CGRect bounds = CGRectMake(0, 0, width, height);
    if (width > kMaxResolution || height > kMaxResolution) {        
        CGFloat ratio = width/height;       
        if (ratio > 1) {            
            bounds.size.width = kMaxResolution;         
            bounds.size.height = roundf(bounds.size.width / ratio);         
        }       
        else {          
            bounds.size.height = kMaxResolution;            
            bounds.size.width = roundf(bounds.size.height * ratio);         
        }       
    }   
    CGFloat scaleRatio = bounds.size.width / width; 
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));   
    CGFloat boundHeight;    
    UIImageOrientation orient = imgPic.imageOrientation;    
    switch(orient) {            
        case UIImageOrientationUp: //EXIF = 1           
            transform = CGAffineTransformIdentity;          
            break;          
        case UIImageOrientationUpMirrored: //EXIF = 2           
            transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);         
            transform = CGAffineTransformScale(transform, -1.0, 1.0);           
            break;          
        case UIImageOrientationDown: //EXIF = 3         
            transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);            
            transform = CGAffineTransformRotate(transform, M_PI);           
            break;          
        case UIImageOrientationDownMirrored: //EXIF = 4         
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);

            transform = CGAffineTransformScale(transform, 1.0, -1.0);

            break;

        case UIImageOrientationLeftMirrored: //EXIF = 5

            boundHeight = bounds.size.height;

            bounds.size.height = bounds.size.width;

            bounds.size.width = boundHeight;

            transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);

            transform = CGAffineTransformScale(transform, -1.0, 1.0);

            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);

            break;

        case UIImageOrientationLeft: //EXIF = 6

            boundHeight = bounds.size.height;

            bounds.size.height = bounds.size.width;

            bounds.size.width = boundHeight;

            transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);

            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);

            break;

        case UIImageOrientationRightMirrored: //EXIF = 7

            boundHeight = bounds.size.height;

            bounds.size.height = bounds.size.width;

            bounds.size.width = boundHeight;

            transform = CGAffineTransformMakeScale(-1.0, 1.0);

            transform = CGAffineTransformRotate(transform, M_PI / 2.0);

            break;

        case UIImageOrientationRight: //EXIF = 8

            boundHeight = bounds.size.height;

            bounds.size.height = bounds.size.width;

            bounds.size.width = boundHeight;

            transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);

            transform = CGAffineTransformRotate(transform, M_PI / 2.0);

            break;

        default:

            [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];

    }

    UIGraphicsBeginImageContext(bounds.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {

        CGContextScaleCTM(context, -scaleRatio, scaleRatio);

        CGContextTranslateCTM(context, -height, 0);

    }

    else {

        CGContextScaleCTM(context, scaleRatio, -scaleRatio);

        CGContextTranslateCTM(context, 0, -height);

    }

    CGContextConcatCTM(context, transform);

    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);

    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();
    CGContextRelease(context);
    CGImageRelease(imgRef);
    return imageCopy;

}

第二种方式:(如果您没有得到上述方法的答案,请使用此方法)

    +(UIImage *)resizeImage:(UIImage *)image toSize:(CGSize)destSize{
        float currentHeight = image.size.height;
        float currentWidth = image.size.width;
        float liChange ;
        CGSize newSize ;
        if(currentWidth == currentHeight) // image is square
        {
            liChange = destSize.height / currentHeight;
            newSize.height = currentHeight * liChange;
            newSize.width = currentWidth * liChange;
        }
        else if(currentHeight > currentWidth) // image is landscape
            {
                liChange  = destSize.width / currentWidth;
                newSize.height = currentHeight * liChange;
                newSize.width = destSize.width;
            }
        else                                // image is Portrait
            {
            liChange = destSize.height / currentHeight;
            newSize.height= destSize.height;
            newSize.width = currentWidth * liChange;
            }


        UIGraphicsBeginImageContext( newSize );
        CGContextRef                context;
        UIImage                     *outputImage = nil;

        context = UIGraphicsGetCurrentContext();
        [image drawInRect:CGRectMake( 0, 0, newSize.width, newSize.height )];
        outputImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        CGImageRef imageRef;
        int x = (newSize.width == destSize.width) ? 0 : (newSize.width - destSize.width)/2;
        int y = (newSize.height == destSize.height) ? 0 : (newSize.height - destSize.height )/2;
        if ( ( imageRef = CGImageCreateWithImageInRect( outputImage.CGImage, CGRectMake(x, y, destSize.width, destSize.height) ) ) ) {
            outputImage = [[UIImage alloc] initWithCGImage: imageRef] ;
        }
        CGImageRelease(imageRef);
        return  outputImage;
    }