找到你要的答案

Q:Repeat image horizontally and vertically

Q:水平和垂直重复图像

I want to repeat the following image:

to achieve the following background:

I tried a few codes as follow:

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 draw image with CGContextDrawTiledImage with no success.

How that can be done?!

EDIT:

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

result:

我想重复下面的图片:

实现以下背景:

我试了几个代码如下:

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

和:

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

我也尝试没有成功CGContextDrawTiledImage绘制图像。

怎么办呢?!

编辑:

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

结果:

answer1: 回答1:

As my comment already said: use

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

You dont want to stretch your image. Let UIKit take care of the repetition.

Assuming that your bluePatternView is the actual large view for which you want to set a pattern as background. Maybe you are setting the background property of the wrong view if that code is still not working.

Note: duplicating the SAME image with @2x and @3x extension will lead to behaive properly for better resolution devices.

正如我的评论已经说过:使用

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

你不想伸展你的形象。让UIKit照顾重复。

假设你的bluepatternview是实际的大视角要为其设置模式为背景。如果代码仍不工作,可能正在设置错误视图的背景属性。

注意:复制相同的图像以@2x和@ 3x扩展将导致behaive正确更好的分辨率的设备。

answer2: 回答2:

I found solution in images.xcassets,

Step 1:- Put your image to images.xcassets

Step 2:- Click on image, then click on bottom right corner written “Show Slicing” Put image

Step 3:- Click on Start Slicing

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

Step 5:- Here you will see 3-Horizontal and 3-Vertical slice lines.

  • Put the most left vertical line to left side of image and both right most lines to right side of image.
  • Put the most top line to top of image and both bottom most lines to bottom of image.
  • So the final result will be looking like this.

Step 6:- Use this image.

And now image will be repeated.

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

Example by Apple

我发现解决images.xcassets,

步骤1:把你的形象images.xcassets

Step 2:- Click on image, then click on bottom right corner written “Show Slicing” Put image

步骤3:点击开始切片

步骤4:点击“水平和垂直切片”按钮

步骤5:在这里你会看到三3-vertical螺旋线。

  • Put the most left vertical line to left side of image and both right most lines to right side of image.
  • Put the most top line to top of image and both bottom most lines to bottom of image.
  • So the final result will be looking like this.

步骤6:使用此图像。

现在图像将重复。

注意:如果你给切片为2x图像会重复刚刚2x图像,3图像需要做同样的切片。

例如苹果

answer3: 回答3:

this with Bellow Method using stretchableImageWithLeftCapWidth like this way:-

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

as par your need example:-

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

  [_scro setBackgroundColor:[UIColor colorWithPatternImage:backgroundImage]];

这stretchableimagewithleftcapwidth波纹管使用这样的方法:—

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

正如你需要的例子:—

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

  [_scro setBackgroundColor:[UIColor colorWithPatternImage:backgroundImage]];
ios  imageview