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

ste*_*ous 7 iphone cocoa-touch core-graphics quartz-graphics uiimage

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

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(context, kCGBlendModeOverlay);
Run Code Online (Sandbox Code Playgroud)

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

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

someImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image-name.png"]];
Run Code Online (Sandbox Code Playgroud)

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

[[UIImage imageNamed:@"image-name.png"] drawInRect:rect blendMode:kCGBlendModeOverlay alpha:1.0];
Run Code Online (Sandbox Code Playgroud)

这个人需要一个CGImage我不确定如何使用PNG:

CGContextDrawImage(context, rect, someCGImage);
Run Code Online (Sandbox Code Playgroud)

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

感谢任何线索.:)

Ken*_*agh 20

您不需要使用UIImageViews.相反,在调用UIGraphicsBeginImageContext和之间括起你的绘图UIGraphicsEndImageContext.

例如(代码未尝试):

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();
Run Code Online (Sandbox Code Playgroud)

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

CGContextRef context = UIGraphicsGetCurrentContext();
Run Code Online (Sandbox Code Playgroud)

获取上下文参考.