How to create a graphic background for UIView so that it does not scale?

In an iOS app, I have a 250x85pt UIView. I would like for him to have a background with image-based tiles. The original image is 398x398 px. I would like my image to be tiled.

enter image description here

When I launch the application, my graphics tiles, but for some reason, it scales as follows:

enter image description here

For this, I used the following code:

var image = UIImage(contentsOfFile: "myfilepath.png")! var uicolor = UIColor(patternImage: image) uiview.backgroundColor = uicolor 

Could you explain 1. why ios scales my image and 2. How to make a tiled image without scaling?

+8
ios swift uiimage
source share
3 answers

basically the image is larger than your UIView . when you give UIColor(patternImage: image) , it uses 1x image size. Usually we, as a rule, laptops, scale to the size of the window with its aspect ratio.

So scale your image as you wish, and then apply it as a background image.

+6
source share

To extend the answer to Banto, the problem is that your view is 250x85 pixels, which on grid devices is really 500x170 pixels. When your x1 image turns into a template, it is applied at the dot level. The task can be easily changed by setting the scale on the image in accordance with the scale on the device:

 let view = UIView(frame: CGRect(x: 0, y: 0, width: 250, height: 85)) let image = UIImage(named: "myfilepath.png")! let scaled = UIImage(CGImage: image.CGImage, scale: UIScreen.mainScreen().scale, orientation: image.imageOrientation) view.backgroundColor = UIColor(patternImage: scaled!) 
+4
source share

try it

  self.mainView.layer.contents = (id)[UIImage imageNamed:@"CROPNET.png"].CGImage; 
0
source share

All Articles