最近因项目中缺少ios开发,临时客串解决一些ios问题。
在一个需求中,获取到照片的数据,将UIImage写入文件,发现生成的图片是斜的。
朝向向右,而不是向上。
而用UIImageView时能显示正常,因为UIImageView会根据图片旋转容器的方向。
而我们是要把图片上传到服务器,所以这个不行。
然后在网上找到的办法如下:(不行)
+ (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) {
caseUIImageOrientationDown:
caseUIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
transform = CGAffineTransformRotate(transform, M_PI);
break;
caseUIImageOrientationLeft:
caseUIImageOrientationLeftMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width,0);
transform = CGAffineTransformRotate(transform, M_PI_2);
break;
caseUIImageOrientationRight:
caseUIImageOrientationRightMirrored:
transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);
transform = CGAffineTransformRotate(transform, -M_PI_2);
break;
default:
break;
}
switch (aImage.imageOrientation) {
caseUIImageOrientationUpMirrored:
caseUIImageOrientationDownMirrored:
transform = CGAffineTransformTranslate(transform, aImage.size.width,0);
transform = CGAffineTransformScale(transform, -1, 1);
break;
caseUIImageOrientationLeftMirrored:
caseUIImageOrientationRightMirrored:
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) {
caseUIImageOrientationLeft:
caseUIImageOrientationLeftMirrored:
caseUIImageOrientationRight:
caseUIImageOrientationRightMirrored:
// 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 = [UIImageimageWithCGImage:cgimg];
CGContextRelease(ctx);
CGImageRelease(cgimg);
return img;
}
该方法不行,在断点调试时发现,是因为该图片UIImage的朝向就是 UIImageOrientationUp。
也就是在已进入这个方法时就return了,下面的都没有执行。
但我们视觉看到的确实是朝向不对,代码检测的却是朝向上。
所以导致该方法不行,我试过去屏蔽掉第一句的判断以及return。但是生成的图片白屏。
还是过其他的办法,发现有的是能旋转过来,但图片的宽高会有问题,导致图片在宽度上被拉伸,高度上被压缩,
即图片的宽高没有对换。
然后发现一个方法: quartz2D来画图片,然后使用ctm变幻来实现旋转
原文地址:https://blog.csdn.net/bitcser/article/details/52055442?utm_source=blogxgwz4
代码如下:
+(UIImage *)image:(UIImage *)image rotation:(UIImageOrientation)orientation
{
long double rotate = 0.0;
CGRect rect;
float translateX = 0;
float translateY = 0;
float scaleX = 1.0;
float scaleY = 1.0;
switch (orientation) {
case UIImageOrientationLeft:
rotate =M_PI_2;
rect =CGRectMake(0,0,image.size.height, image.size.width);
translateX=0;
translateY= -rect.size.width;
scaleY =rect.size.width/rect.size.height;
scaleX =rect.size.height/rect.size.width;
break;
case UIImageOrientationRight:
rotate =3 *M_PI_2;
rect =CGRectMake(0,0,image.size.height, image.size.width);
translateX= -rect.size.height;
translateY=0;
scaleY =rect.size.width/rect.size.height;
scaleX =rect.size.height/rect.size.width;
break;
case UIImageOrientationDown:
rotate =M_PI;
rect =CGRectMake(0,0,image.size.width, image.size.height);
translateX= -rect.size.width;
translateY= -rect.size.height;
break;
default:
rotate =0.0;
rect =CGRectMake(0,0,image.size.width, image.size.height);
translateX=0;
translateY=0;
break;
}
UIGraphicsBeginImageContext(rect.size);
CGContextRef context =UIGraphicsGetCurrentContext();
//做CTM变换
CGContextTranslateCTM(context, 0.0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextRotateCTM(context, rotate);
CGContextTranslateCTM(context, translateX,translateY);
CGContextScaleCTM(context, scaleX,scaleY);
//绘制图片
CGContextDrawImage(context, CGRectMake(0,0,rect.size.width, rect.size.height), image.CGImage);
UIImage *newPic =UIGraphicsGetImageFromCurrentImageContext();
return newPic;
}
网友评论