7

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

4

3 回答 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;
}
于 2013-04-06T11:57:42.317 回答
3

尝试使用这个:

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

代替:

NSData *profileData = UIImagePNGRepresentation(profileImgView.image);

希望它有效。:)

于 2014-09-29T10:04:18.747 回答
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;
    }
于 2013-04-06T11:53:02.050 回答