Why initWithPatternImage loses alpha PNG values

Has anyone seen the problem of [UIColor initWithPatternImage] ignoring PNG alpha values? If so, what is the best fix?

I use png as the background layer for my array of button objects, and it contains several predefined alpha per pixel values โ€‹โ€‹in the image, which should be used as the background texture. It loads as a template / texture color, but the entire transparent area is transparent as an opaque black.

It is important that I get the correct alpha values โ€‹โ€‹so that the button images are displayed correctly. Button frames do not include alpha shadows from the background, as this is not a โ€œclickable" part of the button. In addition, my images of object objects and background images also use transparency, so in fact you need to have a clear background immediately behind each button to ensure the correct current current color settings (the bottom layer of the UIView will have its own background color set to the current color selected by the user ) Setting only one alpha value for a UIView layer containing this texture is also not suitable for my needs.

Any help would be greatly appreciated. My current workaround would be to use a fully bloated, repeatedly programmed layout of multiple UIImageViews using png instead of a single UIView with pattern filling.

Here is a snippet of code, but it is pretty standard for turning UIImage into UIColor for use as template / texture color:

    UIView *selectorView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,320)];
    UIColor *background  = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"SelectorViewBackground.png"]];

    selectorView.backgroundColor = background;

    [mainView addSubview:selectorView]; // pattern background layer. Add UIButtons on top of this selectorView layer
    [self addSubview:mainView]; // current user selected color set in mainView.
    [selectorView release];
    [background release];
+3
source share
4 answers

I had the same problem setting the background on a UIView with some transparency, here is how I solved it:

theView.backgroundColor = [UIColor clearColor];
theView.layer.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"the_image_with_transparancy.png"]].CGColor;
+3
source

This is probably due to:

Tiled Background Image: Can I do this using UIImageView?

Basically, try installing:

[view setOpaque:NO];
[[view layer] setOpaque:NO];
+1
source

, . , , ( , ) .

PNG . , iOS / PNG (, 8- PNG 8- ). , PNG 24 8- ( 32 ).

, , , view/layer PNG?

0

For those who may need a workaround code where background patterns can be laid out as strings in UIScrollView, here it (adjusted to fix privacy issues, should work if the variables are set correctly before the call).

Please note that there should be ways to reuse only one dedicated instance of UIImageView several times to save memory or load time, but time to market is my No. 1 driver right now. Enjoy the trip :)

    UIImageView *selectorView;
    for (int i = 0; i < numRows; ++i) {
        selectorView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SelectorViewBackground.png"]];
        selectorView.frame = CGRectMake(0, i * patternHeight, patternWidth, patternHeight);
        [mainView addSubview:selectorView];
        [selectorView release];         
    }
0
source

Source: https://habr.com/ru/post/1794038/


All Articles