摄像头图像旋转问题

7

我遇到了一个非常奇怪的问题。当我以纵向模式点击图片并上传后,再次获取它时,它会逆时针旋转90度显示。但是,在照片库中查看时,它将显示在正确的方向上,就像拍摄时一样。我尝试了几乎所有可能的链接/代码来解决这个问题,但似乎没有什么帮助。我以JPEG格式保存图像。请有人帮忙解决,万分感谢!

3个回答

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

2
https://dev59.com/5GUp5IYBdhLWcg3wCUGx#15701975 - Arun

3
尝试使用这个:
NSData *profileData =  UIImageJPEGRepresentation(self.profileImgView.image, 1.0); 

而不是:

NSData *profileData = UIImagePNGRepresentation(profileImgView.image);

希望它能正常工作。 :)

1

我希望这对你有所帮助:

在这里,我提供了调整图像大小的方法,同时也可以解决你的问题。

第一种方法:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接