IOS,上传图片旋转90°问题出现原因以及解决方法.

这里小编提出一个项目中遇到的问题,在做个人中心这一块,User的头像需要用户自己传图片上去,要么是相机直接拍照,要么是调用相册中已有的照片,但是图片的大小不同,其中小编在传头像时,较小的图片都能正常上传和请求,但是大一些的图片就会出现没有规律的旋转;
第一,旋转情况的示例:


IOS,上传图片旋转90°问题出现原因以及解决方法._第1张图片

上传前相册图片样子

IOS,上传图片旋转90°问题出现原因以及解决方法._第2张图片

上传后手机应用显示样子(1)

IOS,上传图片旋转90°问题出现原因以及解决方法._第3张图片

(2)

小编在上传图片时图片的位置是正确的,但是上传后图片就会出现如上图的旋转,没有规律,或左或右,这种情况让小编一度很头疼,最后在网上找到了两种解决方法,这里小编就把我知道的方法整合出来,希望大神们的方法,能给遇到类似问题的小伙伴提供帮助(出现这种情况是因为上传的图片大小大于2M,当大于2M时图片就出旋转);
方法一:压缩图片的大小iOS自己也有要锁图片的方法例如UIImageJPEGRepresentation(image,0.1);这个方法不管你后边的倍数写多小,他都会有一个极限,遇到特殊情况,这个方法并不能把图片压倒足够小;小编的压缩方法如下:
-(UIImage *)imageByScalingToMaxSize:(UIImage *)sourceImage{

if (sourceImage.size.widthsourceImage.size.height) {
    btHeight=ORIGINAL_MAX_WIDTH;
    btWidth=sourceImage.size.width *(ORIGINAL_MAX_WIDTH/sourceImage.size.height);
}else {
    btWidth=ORIGINAL_MAX_WIDTH;
    btHeight=sourceImage.size.height * (ORIGINAL_MAX_WIDTH / sourceImage.size.width);
}
CGSize targetSize=CGSizeMake(btWidth, btHeight);
return [self imageByScalingAndCroppingForSourceImage:sourceImage targetSize:targetSize];}

-(UIImage*)imageByScalingAndCroppingForSourceImage:(UIImage *)sourceImage targetSize:(CGSize)targetSize{

UIImage *newImage=nil;
CGSize imageSize=sourceImage.size;
CGFloat width=imageSize.width;
CGFloat height=imageSize.height;
CGFloat targetWidth=targetSize.width;
CGFloat targetHeight=targetSize.height;
CGFloat scaleFactor=0.0;
CGFloat scaledWidth=targetWidth;
CGFloat scaledHeight=targetHeight;
CGPoint thumbnailPoint=CGPointMake(0.0, 0.0);
if (CGSizeEqualToSize(imageSize, targetSize)==NO) {
    CGFloat widthFactor=targetWidth / width;
    CGFloat heightFactor= targetHeight /height;
    
    if (widthFactor > heightFactor) {
        scaleFactor = widthFactor;
    }else{
        scaleFactor = heightFactor;
        scaledWidth = width * scaleFactor;
        scaledHeight = height * scaleFactor;
    }
    // center the image
    if (widthFactor > heightFactor) {
        thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
    }else if ( widthFactor < heightFactor){
        thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
    }
}
UIGraphicsBeginImageContext(targetSize);  // this will crop
CGRect thumbnailRect= CGRectZero ;
thumbnailRect.origin=thumbnailPoint;
thumbnailRect.size.width  = scaledWidth;
thumbnailRect.size.height = scaledHeight;

[sourceImage drawInRect:thumbnailRect];

newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil) NSLog(@"could not scale image");
UIGraphicsEndImageContext();
return newImage;}

这种方法虽然该能解决图片旋转的问题,但是压缩过后的图片会出现严重失真,模糊不清出,小编又找了第二种方法

第二种:对要上传的图片进行位置的调整,在从相机中拿到照片后,对咬传的照片进行调整,如果旋转了就报它在旋转回来方法如下(这个方法我用了类别,写成➕方法来用,加号没识别出来,变成了黑点):

  • (UIImage *)fixOrientation:(UIImage *)aImage {
if (aImage.imageOrientation == UIImageOrientationUp)
    return aImage;

CGAffineTransform transform = CGAffineTransformIdentity;

switch (aImage.imageOrientation) {
    case UIImageOrientationDown:
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
        transform = CGAffineTransformRotate(transform, M_PI);
        break;
        
    case UIImageOrientationLeft:
    case UIImageOrientationLeftMirrored:
        transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
        transform = CGAffineTransformRotate(transform, M_PI_2);
        break;
        
    case UIImageOrientationRight:
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);
        transform = CGAffineTransformRotate(transform, -M_PI_2);
        break;
    default:
        break;
}

switch (aImage.imageOrientation) {
    case UIImageOrientationUpMirrored:
    case UIImageOrientationDownMirrored:
        transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
        transform = CGAffineTransformScale(transform, -1, 1);
        break;
        
    case UIImageOrientationLeftMirrored:
    case UIImageOrientationRightMirrored:
        transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);
        transform = CGAffineTransformScale(transform, -1, 1);
        break;
    default:
        break;
}

CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,
                                         CGImageGetBitsPerComponent(aImage.CGImage), 0,
                                         CGImageGetColorSpace(aImage.CGImage),
                                         CGImageGetBitmapInfo(aImage.CGImage));
CGContextConcatCTM(ctx, transform);
switch (aImage.imageOrientation) {
    case UIImageOrientationLeft:
    case UIImageOrientationLeftMirrored:
    case UIImageOrientationRight:
    case UIImageOrientationRightMirrored:
        // Grr...
        CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);
        break;
        
    default:
        CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);
        break;
}

CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;}

好了以上就是小编的全部方法,如果能帮助你,小编会非常高兴,如有其他方法也欢迎读者指出,小编也非常感谢您的不吝赐教.

你可能感兴趣的:(IOS,上传图片旋转90°问题出现原因以及解决方法.)