UIImage不透明

Adr*_*rli 8 iphone cocoa-touch objective-c

我正在尝试制作一个让用户用手指画画的视图.我在画笔的Pixelmator中制作了一个png.我的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];
}
Run Code Online (Sandbox Code Playgroud)

画笔图像是具有透明度的png,但是当我运行应用程序时,没有透明度.有谁知道为什么,以及如何解决它?

编辑: 我发现图像是透明的,但是当我调用时drawInRect,图像将透明像素绘制为视图的背景颜色.我可以使用CGBlendMode来解决这个问题吗?

Ser*_*tov 5

解决方案很简单,在我自己的项目上进行测试。在您使用自己的背景颜色自定义- (void)drawRect:(CGRect)rect设置进行更新的班级中-(id)init

[self setBackgroundColor:[UIColor clearColor]];

就这样。在我的项目上测试过。


May*_*tin 1

看起来它可能会采用上下文的当前填充颜色。

尝试使用 CGContextSetFillColorWithColor() 将上下文的填充颜色设置为 [UIColor clearColor].CGColor

如果这不起作用,唯一一个简单且不会影响性能的解决方案是拥有 2 个视图:

  • 背景视图 - 这将是具有正确背景颜色或图像的视图
  • 叠加视图 - 此视图将检测触摸等,然后在顶部绘制所有画笔描边。该视图的背景颜色可以是 [UIColorclearColor],因此当您绘制画笔图像时,alpha 将填充为 [UIColorclearColor]。基本上与您现在看到的视图相同,只是背景清晰。

注意:您不需要弄乱混合模式即可实现此目的。您应该能够使用默认的drawInRect:方法