Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. My drawRect: method looks like this:

- (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. 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. Is there a CGBlendMode I can use to fix this?

like image 983
Adrian Sarli Avatar asked Jul 28 '11 18:07

Adrian Sarli


2 Answers

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

[self setBackgroundColor:[UIColor clearColor]];

And thats all. Tested on my project.

like image 72
Serhii Mamontov Avatar answered Sep 28 '22 04:09

Serhii Mamontov


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

If that doesn't work, the only other solution that is simple and shouldn't have a performance hit is to have 2 views:

  • Background View - This will be view that has the proper background color or image
  • Overlay View - This view will detect the touches etc and then draw all of the brush strokes on top. The background color of this view can then be [UIColor clearColor] so when you draw the brush images, the alpha will be filled with [UIColor clearColor]. Basically the same view you have now, just with a clear background.

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

like image 36
MayorJustin Avatar answered Sep 28 '22 06:09

MayorJustin