0

I'm triying to fill with pattern my mask images (.png)

UIImageView *imageView = (UIImageView*)_maskView.contentView;
imageView.image = [imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
imageView.tintColor = [UIColor colorWithPatternImage:image];

Now I have done what I want to do. But with a problem. Here is the screenshot:

screenshot

In this screenshot I have these layers back to front;

  1. mainImageView -> contains the waterfall image.
  2. maskView -> lies on the top of the mainImageView. It is a custom UIView. It contains an imageView named contentView.
  3. contentView -> An UIImageView. Subview of maskView. Contains the mask image. I use colorWithPatternImage code on its tintColor.

As you can see, my pattern image is tiling(or repeating or whatever you call).

I tried to resize the pattern image to the size of the imageView also tried resizing to high resolutions like 4000X4000 with no luck.

Tried something like

self.layer.contents = (id)[UIImage imageNamed:@"freshBackground.png"].CGImage;

but this code is filling the entire imageView.

Now I'm out of options. Can anyone help me on this? Thank you.

Arda Oğul Üçpınar
  • 881
  • 1
  • 14
  • 38
  • Where are you calling self.layer.contents = (id)[UIImage imageNamed:@"freshBackground.png"].CGImage; ? – Vijay Tholpadi Dec 29 '14 at 18:29
  • why not make your background view an image view? alternatively, you could also override drawRect in your background view and use CGContextDrawImage. – Mike M Dec 29 '14 at 18:57
  • @Vijayts I delete imageView.image = [imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; imageView.tintColor = [UIColor colorWithPatternImage:image]; and used it. – Arda Oğul Üçpınar Dec 29 '14 at 19:05
  • @MikeM You didn't understand the situation well. The tiled image is not at the background. The photo-thing-shaped mask is a .png file. It's photo-thing-shaped part is transparent and other parts are opaque. I'm painting it's opaque part with color or colorWithpatternImage. So at the end I have a shape, masking the main photo, bordered with color or colorWithpatternImage. – Arda Oğul Üçpınar Dec 29 '14 at 19:09

1 Answers1

0

I can confirm that I have not had good a good experience using colorWithPattern with big images. It has always been designed for usage with small images. It especially fails to load 2x and 3x images by itself. During my implementation I used,

self.view.layer.contents = (id)[UIImage imageNamed:@"freshBackground.png"].CGImage;

Regarding the image getting applied to the whole mainView, you can also consider the approach creating a beizerPath and subtracting it from your mainView as shown here instead of using the mask.

Community
  • 1
  • 1
Vijay Tholpadi
  • 2,135
  • 1
  • 15
  • 20