[英]UIImage Not Transparent
I am trying to make a view that lets a user draw with their finger. 我正在尝试制作一个视图,使用户可以用手指绘画。 I have a png made in Pixelmator of the brush.
我在画笔的Pixelmator中制作了一个png。 My drawRect: method looks like this:
我的drawRect:方法看起来像这样:
- (void)drawRect:(CGRect)rect
{
NSAssert(brush != nil, @"");
for (UITouch *touch in touchesNeedingDrawing)
{
//Draw the touch
[brush drawInRect:CGRectWithPoints([touch locationInView:self], [touch previousLocationInView:self]) blendMode:0 alpha:1];
}
[touchesNeedingDrawing removeAllObjects];
}
The brush image is a png with transparency, but when I run the app, there is no transparency. 笔刷图像是具有透明性的png,但是当我运行应用程序时,没有透明性。 Does anyone know why, and how to fix it?
有谁知道为什么,以及如何解决它?
Edit: I discovered that the image is transparent, but when I call drawInRect
, the image draws the transparent pixels as the background color of the view. 编辑:我发现图像是透明的,但是当我调用
drawInRect
,图像将透明像素绘制为视图的背景色。 Is there a CGBlendMode I can use to fix this? 我可以使用CGBlendMode来解决此问题吗?
Solution is very simple, testes on my own project. 解决方案非常简单,请参加我自己的项目。 In class which you updated with custom
- (void)drawRect:(CGRect)rect
set in -(id)init
background color for self 在您使用自定义
- (void)drawRect:(CGRect)rect
在-(id)init
设置的更新的类中,为自己-(id)init
背景色
[self setBackgroundColor:[UIColor clearColor]];
And thats all. 就这样。 Tested on my project.
测试了我的项目。
It seems like it could be taking the current fill color of the context. 好像是采用上下文的当前填充颜色。
Try setting the fill color for the context with CGContextSetFillColorWithColor() to [UIColor clearColor].CGColor 尝试使用CGContextSetFillColorWithColor()将上下文的填充颜色设置为[UIColor clearColor] .CGColor
If that doesn't work, the only other solution that is simple and shouldn't have a performance hit is to have 2 views: 如果这不起作用,那么唯一简单且不应该影响性能的解决方案是拥有2个视图:
Note: You shouldn't need to mess with the blend mode to make this work. 注意:您无需弄混混合模式即可完成这项工作。 You should be able to use the default drawInRect: method
您应该能够使用默认的drawInRect:方法
Is the brush png loaded to an imageView? 画笔png是否已加载到imageView? That is, the variable brush is an object of UIImageView, isn't it?
也就是说,变量brush是UIImageView的对象,不是吗? If so, perhaps simple
如果是这样,也许很简单
brush.backgroundColor = [UIColor clearColor];
will help 会有所帮助
i think you should try destination out blend mode: kCGBlendModeDestinationOut
. 我认为您应该尝试目的地混合模式:
kCGBlendModeDestinationOut
。
You could also draw at point instead draw in rect: 您也可以在点上绘制,而不是在rect中绘制:
[brush drawAtPoint:[touch locationInView:self] blendMode:kCGBlendModeDestinationOut alpha:1]
A possible issue is that you are setting the blendMode to 0. I suggest using the -drawInRect:
method without a blend mode. 一个可能的问题是您将blendMode设置为0。我建议使用不带混合模式的
-drawInRect:
方法。 The issue may also be that your view has a black background, but that is doubtful. 问题可能还在于您的视图具有黑色背景,但这令人怀疑。 I would also suggest attempting to display the UIImage in a UIImageView as a test.
我也建议尝试在UIImageView中显示UIImage作为测试。 The issue may be related to the way that PixelMator exports images.
该问题可能与PixelMator导出图像的方式有关。
Your problem is a fundamental misconception of how drawRect:
actually works. 您的问题是对
drawRect:
实际上如何工作的根本误解。 Every time you draw something into the current graphics context, everything that was there previously will be cleared (so that only the backgroundColor
remains). 每次在当前图形上下文中绘制某些内容时,以前存在的所有内容都会被清除(从而仅保留
backgroundColor
)。
Since you're only drawing the current touch(es) ( touchesNeedingDrawing
is emptied), there's nothing under the rectangle you're filling that could show the transparency of the image you're drawing, so the background color shows through. 由于您仅绘制当前触摸(
touchesNeedingDrawing
为空),因此,您正在填充的矩形下没有任何内容可以显示所绘制图像的透明度,因此背景色可以显示出来。
You basically have two options to resolve this: 您基本上可以通过两种方法解决此问题:
1) Keep all touches that contribute to the drawing around (don't empty the touchesNeedingDrawing
array) and redraw all of them every time – this is going to be easy but quite slow. 1)保留所有有助于绘图的接触(不要清空
touchesNeedingDrawing
数组),并每次都重新绘制所有接触–这很容易,但速度很慢。
2) Draw into a buffer of some kind (eg a UIImage
, using UIGraphicsBeginImageContext
etc.). 2)绘制某种缓冲区(例如,使用
UIGraphicsBeginImageContext
等的UIImage
)。 Every time your drawing changes, create a new buffer. 每当您的图形更改时,请创建一个新的缓冲区。 Draw the old buffer into the new buffer and the images for the new stroke on top of it.
将旧缓冲区绘制到新缓冲区中,并在其上方绘制新笔划的图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.