这个问题让我丧命。我不知道出了什么问题。但是下面的代码会将图像颠倒过来。它实际上是垂直翻转的,我不知道为什么。
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();这将导致以下情况:

为什么,为什么?
发布于 2015-01-08 20:33:17
作为一个评论也许比作为一个回答更合适,但对于评论来说太长了。
正如@Andrea所指出的,您同时创建一个CGContext和CIContext有点奇怪。如果您只想从UIImage中提取一个CGImageRef,您可以使用
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);https://stackoverflow.com/questions/27848194
复制相似问题