Repeat image horizontally and vertically

I want to repeat the following image:

enter image description here

to get the following background:

enter image description here

I tried several codes:

bluePatternView.backgroundColor = [UIColor colorWithPatternImage: [[UIImage imageNamed:@"blue_pattern.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:0]]; 

and

 bluePatternView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"blue_pattern.png"]]; 

I also tried to make the image with CGContextDrawTiledImage without success.

How can I do that?!

EDIT: result of the implementation of luk2302. please give him recognition by promoting

 bluePatternView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue_pattern.png"]]; 

result:

enter image description here

+8
source share
5 answers

As mentioned in my comment: use

 bluePatternView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue_pattern.png"]]; 

You do not want to stretch the image. Let UIKit take care of the repetition.

Assuming your bluePatternView is actually a great view for which you want to set the template as the background. You might be setting the background property of the misconception if this code still doesn't work.

Note. Duplicating a SAME image with the @ 2x and @ 3x extensions will result in the correct behavior for higher resolution devices.

enter image description here

+7
source

I found a solution in images.xcassets ,

Step 1: Put the image in images.xcassets

Step 2: - Click on the image, then click on the lower right corner that says "Show Slicing" Put image

Step 3: - Click "Start slicing"
start slicing

Step 4: - Click the "Slice Horizontally and Vertically" button

Slice horizontally and vertically

Step 5: Here you will see 3-horizontal and 3-vertical cut lines.
enter image description here

  • Place the leftmost vertical line on the left side of the image and as the right side of the lines on the right side of the image.
  • Place the topmost line at the top of the image and the bottom at the bottom of the image.
  • Thus, the final result will be as follows.

enter image description here

Step 6: Use this image.

And now the image will be repeated.

enter image description here

Note. If you give a 2x image slicing, it will repeat only a 2x image, for 3x images you need to do the same slicing.

Apple example

+21
source
 bluePatternView.backgroundColor = UIColor(patternImage: UIImage(named: "blue_pattern")!) 
0
source

You need to save the image in Assets and then put these lines in viewdidload

 bluePatternView.backgroundColor = UIColor.init(patternImage: #imageLiteral(resourceName: "f8")) 

This is for Swift 4.1.

0
source

this using the Bellow method using stretchableImageWithLeftCapWidth as follows: -

 UIImage *backgroundImage = [[UIImage imageNamed:@"SheetBackground.png"] stretchableImageWithLeftCapWidth:0.5 topCapHeight:0]; 

as an example of your example: -

 UIImage *backgroundImage = [[UIImage imageNamed:@"q4Ses.png"] stretchableImageWithLeftCapWidth:0.5 topCapHeight:0]; [_scro setBackgroundColor:[UIColor colorWithPatternImage:backgroundImage]]; 
-1
source

All Articles