图像旋转问题的补充(jpg转png和fixOrientation)已解决

继上一篇提到的两个方法,今天发现问题复现了,上述两个方法肯定是用不了了
我去比对了一下从imagePicker中经过编辑后的图片的属性与相机拍出来的照片的差异。
经过编辑后,图像不再是jpg格式,而是png,而png图像不包含图像方向的信息,因此检测问题都正常
所以想通过图像转换jpg->png

UIImage * image = [UIImage imageNamed:@"1.jpg"];
NSData * data = UIImagePNGRepresentation(image);
UIImage * imagePng = [UIImage imageWithData:data];
//补充一下png->jpg
//1.参数2--> 质量因子. 范围:0-1之间.(0不清楚,1最清楚)
//2.质量因子越小,图片越小.但是越不清晰.
NSData * data = UIImageJPEGRepresentation(image, 1);
UIImage * imagePng = [UIImage imageWithData:data];

但是这样不是解决问题的根本方法啊,只能暂时去处理掉这个问题,难不成以后每次都要这样吗?思考了一会,感觉应该是方向信息的问题,查看图片的差异发现
正常的
在这里插入图片描述
不正常的
在这里插入图片描述
无奈只能去做搞一下图片的方向信息问题。代码如下

- (UIImage *)fixOrientation:(UIImage *)aImage {
    // No-op if the orientation is already correct
    if (aImage.imageOrientation == UIImageOrientationUp)
        return aImage;
    // We need to calculate the proper transformation to make the image upright.
    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
    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;
    }
   
    // Now we draw the underlying CGImage into a new context, applying the transform
    // calculated above.
    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;
    }
   
    // And now we just create a new UIImage from the drawing context
    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
}



详细介绍就不介绍了,调用就行了。
再重复补充一下方向信息:

UIImageOrientationUp,            // 默认方向
UIImageOrientationDown,          // 让默认方向旋转180度
UIImageOrientationLeft,          // 让默认方向逆时针旋转90度
UIImageOrientationRight,         // 让默认方向顺时针旋转90度
UIImageOrientationUpMirrored,    // 默认方向的竖线镜像
                                 //(即以原图的左(或右)边的竖线为对称轴,对原图进行对称投影得到的镜像)
UIImageOrientationDownMirrored,  // 让镜像旋转180度
UIImageOrientationLeftMirrored,  // 让镜像逆时针旋转90度
UIImageOrientationRightMirrored, // 让镜像顺时针旋转90度

总结一下:这篇文章没有好好的去写,记录一下解决的过程,遇到问题,不要想怎么去规避,因为规避了一次,下一次可能还会出来,最好的办法就是解决问题,我已经在这个地方遇到坑了,下次解决问题的方法,就从为什么产生这个问题开始。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值