gpt4 book ai didi

iphone - CGContext的垂直翻转

转载 作者:行者123 更新时间:2023-12-03 18:14:45 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com