首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CGContextRef => CGImageRef => CIImage => CGImageRef => CGContextDrawImage链将图像颠倒

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

Stack Overflow用户
提问于 2015-01-08 19:36:33
回答 1查看 1.5K关注 0票数 1

这个问题让我丧命。我不知道出了什么问题。但是下面的代码会将图像颠倒过来。它实际上是垂直翻转的,我不知道为什么。

代码语言:javascript
复制
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();

这将导致以下情况:

为什么,为什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-08 20:33:17

作为一个评论也许比作为一个回答更合适,但对于评论来说太长了。

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

代码语言:javascript
复制
UIImage *newImage = [[UIImage alloc] initWithCGImage:cgImg]

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

代码语言:javascript
复制
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0, CGBitmapContextGetHeight(context));
CGContextScaleCTM(context, 1, -1);
// All your drawing code goes here.
CGContextRestoreGState(context);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27848194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档