将PNG图像绘制到混合模式操作的图形上下文中 [英] Drawing a PNG Image Into a Graphics Context for Blending Mode Manipulation
问题描述
我需要在 CGContext
中绘制一系列PNG,以便将它们混合在一起:
I need to draw a series of PNGs into a CGContext
so I can blend them together:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(context, kCGBlendModeOverlay);
目前我的应用程序基于将每个图像绘制为UIImageView并将其添加为子视图到视图文件: [self addSubview:someImageView]
...但是这不允许混合模式操作,对吗?
Currently I have my app working based on just drawing each image as a UIImageView and adding them as a subview to the view file via: [self addSubview:someImageView]
...but this doesn't allow for blending mode manipulation, right?
目前UIImageView的vars是通过以下方式分配的:
Currently the UIImageView vars are being assigned via:
someImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image-name.png"]];
所以我试着用没有运气的替换这些:
So I've tried replacing those with either of these with no luck:
[[UIImage imageNamed:@"image-name.png"] drawInRect:rect blendMode:kCGBlendModeOverlay alpha:1.0];
,这需要一个 CGImage
不知道如何使用PNG:
and this one needs a CGImage
which I'm not sure how to make with the PNGs:
CGContextDrawImage(context, rect, someCGImage);
那么我缺少什么?这些不是由任何互动触发,他们只需要加载/绘制应用程序加载时。
So what am I missing? These aren't being triggered by any interaction, they just need to load/draw when the app loads.
感谢任何线索。 :)
推荐答案
您不需要使用UIImageView。
相反,在调用 UIGraphicsBeginImageContext
和 UIGraphicsEndImageContext
之间加上你的绘图。
You don't need to be using UIImageViews.
Instead, bracket your drawing between calls to UIGraphicsBeginImageContext
and 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();
如果要使用CG调用,请调用:
If you want to use CG calls, call:
CGContextRef context = UIGraphicsGetCurrentContext();
获取上下文引用。
这篇关于将PNG图像绘制到混合模式操作的图形上下文中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!