Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c UIView的背景缩放不正确且偏移_Objective C_Ios - Fatal编程技术网

Objective c UIView的背景缩放不正确且偏移

Objective c UIView的背景缩放不正确且偏移,objective-c,ios,Objective C,Ios,我看到了其他帖子,但仍然无法使我的背景图像正确缩放。我使用UIView设置背景图像,因为我需要背景图像垂直平铺。我将UIView的帧设置为与png的像素宽度和高度相同的大小,但图像偏移和缩放不正确。可能是因为图像有一些透明像素吗?我尝试了以下方法: _view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]; _view.opaque = NO; _view.cont

我看到了其他帖子,但仍然无法使我的背景图像正确缩放。我使用UIView设置背景图像,因为我需要背景图像垂直平铺。我将UIView的帧设置为与png的像素宽度和高度相同的大小,但图像偏移和缩放不正确。可能是因为图像有一些透明像素吗?我尝试了以下方法:

_view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
_view.opaque = NO;
_view.contentMode = UIViewContentModeScaleAspectFit;
//_view.layer.contentsGravity = kCAGravityTop;

我认为这是因为你使用的是图像作为背景色。 如果使用imageview并将其添加为子视图(如下图所示),效果会更好

//编辑以进行垂直平铺

 int y = 0;
int imgsize = 100; //in pixels
for (i =0; i<max; i++) { //max is the number of tiles you want
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
img.frame = CGRectMake(0,y+imgsize,100,100);
y+=imgsize; //increase y each time(new starting point)
[_view addSubview:img];
}
inty=0;
int imgsize=100//以像素为单位

对于(i=0;i我认为这是因为您使用图像作为背景色。 如果使用imageview并将其添加为子视图(如下图所示),效果会更好

//编辑以进行垂直平铺

 int y = 0;
int imgsize = 100; //in pixels
for (i =0; i<max; i++) { //max is the number of tiles you want
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background.png"]];
img.frame = CGRectMake(0,y+imgsize,100,100);
y+=imgsize; //increase y each time(new starting point)
[_view addSubview:img];
}
inty=0;
int imgsize=100;//以像素为单位

对于(i=0;i您可能会发现创建
UIView
的子类更容易。在子类中,重写
drawRect:
以使用
drawAsPatterInRect:
绘制图像。该方法将通过平铺图像填充视图:

- (void)drawRect:(CGRect)dirtyRect {
    [self.tileImage drawAsPatternInRect:self.bounds];
}

您可能会发现创建
UIView
的子类更容易。在子类中,重写
drawRect:
以使用
drawAsPatterInRect:
绘制图像。该方法将通过平铺图像填充视图:

- (void)drawRect:(CGRect)dirtyRect {
    [self.tileImage drawAsPatternInRect:self.bounds];
}

你能显示屏幕截图吗?你能显示屏幕截图吗?对不起,我没有正确阅读问题。编辑了我的原始答案以给你一个平铺效果。使用
[uicolorWithPatternImage:image]
将具有更好的性能-这是一种非常暴力的图像平铺方式,但它避开了所有用于绘制图像的最佳CoreGraphics例程(
[图像大小调整图像带插图:{0,0,0,0}resizingMode:UIImageResizingMode]
可以轻松创建平铺图像)抱歉,我没有正确阅读问题。编辑了我的原始答案以提供平铺效果。使用
[uicolorWithPatternImage:image]
将有更好的性能-这是一种非常暴力的平铺图像的方法,但它避开了所有用于绘制图像的最佳CoreGraphics例程(代码>[image resizebleimagewithcapinset:{0,0,0,0}resizengmode:uiimageresizengmodetile]
可以轻松创建平铺图像)