将PNG图像绘制到图形上下文中以进行混合模式操作

时间:2009-12-20 19:05:58

标签: iphone cocoa-touch uiimage core-graphics quartz-graphics

我需要将一系列PNG绘制到CGContext中,以便将它们混合在一起:

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(context, kCGBlendModeOverlay);

目前我的应用程序基于将每个图像绘制为UIImageView,并通过以下方式将它们作为子视图添加到视图文件中:[self addSubview:someImageView] ...但这不允许混合模式操作,对?

目前通过以下方式分配UIImageView变量:

someImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image-name.png"]];

所以我尝试用其中任何一个替换那些没有运气的那些:

[[UIImage imageNamed:@"image-name.png"] drawInRect:rect blendMode:kCGBlendModeOverlay alpha:1.0];

并且这个需要一个CGImage,我不知道如何使用PNG:

CGContextDrawImage(context, rect, someCGImage);

那我错过了什么?这些不是由任何交互触发的,它们只需要在应用加载时加载/绘制。

感谢任何线索。 :)

1 个答案:

答案 0 :(得分:20)

您不需要使用UIImageViews。 相反,请在调用UIGraphicsBeginImageContextUIGraphicsEndImageContext之间对您的绘图进行括号。

例如(代码未尝试):

UIImage* uiimage = [UIImage imageNamed:@"image-name.png"];
UIImage* uiimage2 = [UIImage imageNamed:@"other-image-name.png"];

CGSize size = uiimage.size; 

UIGraphicsBeginImageContext(size);

[uiimage drawAtPoint:CGPointZero blendMode:kCGBlendModeOverlay alpha:1.0];
[uiimage2 drawAtPoint:CGPointZero blendMode:kCGBlendModeOverlay alpha:1.0];

UIImage* blendedImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

如果您想使用CG电话,请致电:

CGContextRef context = UIGraphicsGetCurrentContext();

获取上下文参考。

相关问题