Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preserve image transparency when using colorWithPatternImage:

I have an image prepared with transparency, like this:

alt text

With two UIviews, I configure the background colors as so:

self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
self.dashedView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"red_square.png"]];

I expect the red square to be repeated over the UIView with transparency preserved, but it's been filled by a solid color like this:

alt text

I don't understand why. Is there a simple way to draw a tiled image with transparency? Or do I need to look at drawing Core Graphics patterns?

like image 265
msmithstubbs Avatar asked Nov 17 '10 21:11

msmithstubbs


3 Answers

Pattern images should be keeping the transparency just fine.

Try [self.dashedView setOpaque:NO]

like image 115
Joshua Weinberg Avatar answered Nov 16 '22 21:11

Joshua Weinberg


all you have to do is set opaque to NO after setting the colorWithPatternImage.

self.dashedView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"red_square.png"]];
self.dashedView.opaque = NO;
like image 5
Hulk_SMASH Avatar answered Nov 16 '22 22:11

Hulk_SMASH


Yar, thanks for the feedback.

I found this only happens on iOS 4.3.x but not on iOS 5.0.x. So on 4.3.x I had to do what Yar id and set opaque to NO, then set background image, then set to YES.

UIButton* cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
cancelButton.opaque = YES; // fix for strange issue in 4.x, need to set YES, set bg color image, then set back to NO
cancelButton.backgroundColor = [UIColor colorWithPatternImage: cancelImage];
cancelButton.opaque = NO;
like image 3
seenickcode Avatar answered Nov 16 '22 22:11

seenickcode