图像正在旋转,因为您将图像保存为JPEG,如果您将图像保存在PNG中,则方向不会改变.这里是解决方向问题的代码.
原文链接:https://www.f2er.com/php/131220.html- (UIImage *)fixrotation:(UIImage *)image{ if (image.imageOrientation == UIImageOrientationUp) return image; CGAffineTransform transform = CGAffineTransformIdentity; switch (image.imageOrientation) { case UIImageOrientationDown: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform,image.size.width,image.size.height); transform = CGAffineTransformRotate(transform,M_PI); break; case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: transform = CGAffineTransformTranslate(transform,0); transform = CGAffineTransformRotate(transform,M_PI_2); break; case UIImageOrientationRight: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform,-M_PI_2); break; case UIImageOrientationUp: case UIImageOrientationUpMirrored: break; } switch (image.imageOrientation) { case UIImageOrientationUpMirrored: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform,0); transform = CGAffineTransformScale(transform,-1,1); break; case UIImageOrientationLeftMirrored: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform,image.size.height,1); break; case UIImageOrientationUp: case UIImageOrientationDown: case UIImageOrientationLeft: case UIImageOrientationRight: break; } // Now we draw the underlying CGImage into a new context,applying the transform // calculated above. CGContextRef ctx = CGBitmapContextCreate(NULL,CGImageGetBitsPerComponent(image.CGImage),CGImageGetColorSpace(image.CGImage),CGImageGetBitmapInfo(image.CGImage)); CGContextConcatCTM(ctx,transform); switch (image.imageOrientation) { case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: case UIImageOrientationRight: case UIImageOrientationRightMirrored: // Grr... CGContextDrawImage(ctx,CGRectMake(0,image.size.width),image.CGImage); break; default: CGContextDrawImage(ctx,image.size.height),image.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; }