我有一个 UIView,我正在尝试使用 [CALayer renderInContext:] 将其渲染为 UIImage。然而,我发现生成的图像是垂直翻转的。由于坐标系不同,我有点期待这一点。但是,然后我尝试使用仿射变换将上下文翻转回正常状态 - 但它没有任何效果:
CGAffineTransform flipVertical = CGAffineTransformMake(
1, 0, 0, -1, 0, imageContextHeight
);
CGContextConcatCTM(imageContext, flipVertical);
CGImageRef cgImage = CGBitmapContextCreateImage(imageContext);
UIImage* uiImage = [[UIImage imageWithCGImage:cgImage] retain];
CGImageRelease(cgImage);
我做错了什么?
最佳答案
这是我根据这个答案编写的一些代码,以防对任何人有帮助:
#import <QuartzCore/QuartzCore.h>
...
+ (UIImage *) flipImageVertically:(UIImage *)originalImage {
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:originalImage];
UIGraphicsBeginImageContext(tempImageView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGAffineTransform flipVertical = CGAffineTransformMake(
1, 0, 0, -1, 0, tempImageView.frame.size.height
);
CGContextConcatCTM(context, flipVertical);
[tempImageView.layer renderInContext:context];
UIImage *flipedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[tempImageView release];
return flipedImage;
}
关于iphone - CGContext的垂直翻转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1135631/