iphone - CGContext的垂直翻转

标签 iphone uikit core-graphics

我有一个 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/

相关文章:

Javascript Replace() 在 iPhone 上对于长字符串表现不佳

android - Trigger.io 原生插件和源代码控制

ios - CollectionView中的程序化搜索栏实现问题

swift - SwiftUI 中是否有等效的 openSettingsURLString ?

ios - 如何使用 drawRect 方法检测绘制项目上的触摸事件?

iphone - size_t 持有什么样的数据?

iphone - 如何在 iPhone 中设置表格 View 单元格(基于导航的应用程序)的字体样式

iphone - 在 Iphone 的自定义选项卡面板中集成 Kal 日历

ios - 带有层角半径和阴影的 UILabel

ios - 如何提高渲染图像的性能?