gpt4 book ai didi

ios - CGContextRef => CGImageRef => CIImage => CGImageRef => CGContextDrawImage 链将图像颠倒

转载 作者:行者123 更新时间:2023-11-29 10:34:25 29 4
gpt4 key购买 nike

这个问题让我很生气。我不知道出了什么问题。但是下面的代码把图像颠倒了。它实际上进行了垂直翻转,我不知道为什么。

UIFont *font = [UIFont fontWithName:fontName size:fontSize];

NSMutableDictionary *attributes = [[NSMutableDictionary alloc] init];
[attributes setObject:font forKey:NSFontAttributeName];
[attributes setObject:[NSNumber numberWithFloat:kStrokeWidth] forKey:NSStrokeWidthAttributeName];
[attributes setObject:[UIColor redColor] forKey:NSStrokeColorAttributeName];
[attributes setObject:style forKey:NSParagraphStyleAttributeName];

[text drawInRect:drawRect withAttributes:attributes];

[attributes removeObjectForKey:NSStrokeWidthAttributeName];
[attributes removeObjectForKey:NSStrokeColorAttributeName];
[attributes setObject:[UIColor blueColor] forKey:NSForegroundColorAttributeName];
[text drawInRect:drawRect withAttributes:attributes];

CGImageRef cgImg = CGBitmapContextCreateImage(context);
CIImage *beginImage = [CIImage imageWithCGImage:cgImg];

CIContext *cicontext = [CIContext contextWithOptions:nil];
CGImageRef cgimg = [cicontext createCGImage:beginImage fromRect:[beginImage extent]];
CGContextDrawImage(context, [beginImage extent] , cgimg);
CGImageRelease(cgImg);
CGImageRelease(cgimg);


UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

这将导致:

enter image description here

为什么,为什么?

最佳答案

可能作为评论比作为答案更合适,但对于评论来说太长了。

正如@Andrea 指出的那样,您同时创建 CGContext 和 CIContext 有点奇怪。如果您只想从 CGImageRef 中提取 UIImage,您可以使用

UIImage *newImage = [[UIImage alloc] initWithCGImage:cgImg]

生成的 newImage仍然被翻转。 UIImages 和 CGContextRefs 使用的坐标系具有相反方向的垂直轴。我建议您在绘制时垂直翻转初始 CGContextRef:

CGContextSaveGState(context);
CGContextTranslateCTM(context, 0, CGBitmapContextGetHeight(context));
CGContextScaleCTM(context, 1, -1);
// All your drawing code goes here.
CGContextRestoreGState(context);

关于ios - CGContextRef => CGImageRef => CIImage => CGImageRef => CGContextDrawImage 链将图像颠倒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27848194/

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