CGContextRef => CGImageRef => CIImage => CGImageRef => CGContextDrawImage 链将图像颠倒
CGContextRef => CGImageRef => CIImage => CGImageRef => CGContextDrawImage chain turns image upside down
这个问题让我很生气。我不知道出了什么问题。但是下面的代码把图像颠倒了。其实就是垂直翻转,不知道为什么。
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();
这将导致:
为什么,为什么?
这个 可能 作为评论比作为答案更合适,但是评论太长了。
正如@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);
这个问题让我很生气。我不知道出了什么问题。但是下面的代码把图像颠倒了。其实就是垂直翻转,不知道为什么。
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();
这将导致:
为什么,为什么?
这个 可能 作为评论比作为答案更合适,但是评论太长了。
正如@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);