如何在使用colorWithPatternImage时保持图像的透明度:

时间:2021-12-13 23:24:56

I have an image prepared with transparency, like this:

我有一张透明的图片,像这样:

如何在使用colorWithPatternImage时保持图像的透明度:

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

使用两个uiview,我配置背景颜色如下:

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:

我希望红场在UIView上重复保留透明度,但它是用这样的纯色填充的:

如何在使用colorWithPatternImage时保持图像的透明度:

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?

我不明白为什么。有没有一种简单的方法来画一个透明的瓷砖图像?或者我是否需要考虑绘制核心图形模式?

3 个解决方案

#1


25  

Pattern images should be keeping the transparency just fine.

模式图像应该保持透明。

Try [self.dashedView setOpaque:NO]

试着自我。dashedView setOpaque:没有)

#2


5  

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

在设置colorWithPatternImage之后,您所要做的就是将不透明设置为NO。

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

#3


3  

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.

我发现这只在ios4.3中发生。但是iOS 5.0。x上没有。4.3。我要做的是使用Yar id将不透明设置为NO,然后设置背景图像,然后设置为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;

#1


25  

Pattern images should be keeping the transparency just fine.

模式图像应该保持透明。

Try [self.dashedView setOpaque:NO]

试着自我。dashedView setOpaque:没有)

#2


5  

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

在设置colorWithPatternImage之后,您所要做的就是将不透明设置为NO。

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

#3


3  

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.

我发现这只在ios4.3中发生。但是iOS 5.0。x上没有。4.3。我要做的是使用Yar id将不透明设置为NO,然后设置背景图像,然后设置为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;