如何为UIView制作基于图像的平铺背景,使其不能缩放?

时间:2021-09-20 07:00:02

In iOS application, I have 250x85pt UIView. I would like it to have image-based tiled background. The original image is 398x398 px. I would like my image to tile.

在iOS应用程序中,我有250x85pt UIView。我希望它有基于图像的平铺背景。原始图像为398x398像素。我希望我的图像能够平铺。

如何为UIView制作基于图像的平铺背景,使其不能缩放?

When I run application, my image tiles, but for some reason it scales as follows:

当我运行应用程序时,我的图像拼贴,但由于某种原因,它按如下方式缩放:

如何为UIView制作基于图像的平铺背景,使其不能缩放?

I used the following code to achieve it:

我使用以下代码来实现它:

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 do I draw tiled image without scaling?

你能解释一下1.为什么ios会缩放我的图像,以及2.如何在不缩放的情况下绘制平铺图像?

3 个解决方案

#1


6  

basically the image is bigger than your UIView. when you give UIColor(patternImage: image) it uses 1x size of the image. The image we usually laptops are scaled to window size with its aspect ratio.

基本上图像比你的UIView大。当你给UIColor(patternImage:image)时,它使用1x大小的图像。我们通常将笔记本电脑的图像按其宽高比缩放到窗口大小。

So scale your image to your requirement and then apply it as background image.

因此,根据您的要求缩放图像,然后将其应用为背景图像。

#2


4  

To expand on Banto's answer, the issue is that your view is 250x85 points, which, on retina devices is really 500x170 pixels. When your x1 image is turned into a pattern it is applied at the point level. The problem can be easily changed by setting the scale on the image to match the scale on the device:

为了扩展Banto的答案,问题是你的视图是250x85点,在视网膜设备上真的是500x170像素。当您的x1图像变为图案时,它将应用于点级别。通过在图像上设置刻度以匹配设备上的刻度,可以轻松更改问题:

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!)

#3


0  

Try this

尝试这个

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

#1


6  

basically the image is bigger than your UIView. when you give UIColor(patternImage: image) it uses 1x size of the image. The image we usually laptops are scaled to window size with its aspect ratio.

基本上图像比你的UIView大。当你给UIColor(patternImage:image)时,它使用1x大小的图像。我们通常将笔记本电脑的图像按其宽高比缩放到窗口大小。

So scale your image to your requirement and then apply it as background image.

因此,根据您的要求缩放图像,然后将其应用为背景图像。

#2


4  

To expand on Banto's answer, the issue is that your view is 250x85 points, which, on retina devices is really 500x170 pixels. When your x1 image is turned into a pattern it is applied at the point level. The problem can be easily changed by setting the scale on the image to match the scale on the device:

为了扩展Banto的答案,问题是你的视图是250x85点,在视网膜设备上真的是500x170像素。当您的x1图像变为图案时,它将应用于点级别。通过在图像上设置刻度以匹配设备上的刻度,可以轻松更改问题:

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!)

#3


0  

Try this

尝试这个

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