[英]How to centre the background image in a UIView without streching it
I want to centre the background image in my UIView
. 我想在
UIView
居中背景图像。 The background image is smaller than the UIView
itself, and I do not want to stretch it, ie I want it to have its original size. 背景图像小于
UIView
本身,并且我不想拉伸它,即我希望它具有其原始大小。 I wrote the following code 我写了下面的代码
UIImage *backgroundImage=[[UIImage alloc] initWithContentsOfFile:@"analog_display_bgr.png"];
CGSize backgroundImageSize= CGSizeMake (backgroundImage.size.width,backgroundImage.size.height);
UIGraphicsBeginImageContext(backgroundImageSize);//self.frame.size
CGRect backgroundImagePosition = CGRectMake(self.bounds.origin.x+m_center.x-backgroundImage.size.width/2.0, self.bounds.origin.y+m_center.y-backgroundImage.size.height/2.0, backgroundImage.size.width, backgroundImage.size.height);
[[UIImage imageNamed:@"analog_display_bgr.png"] drawInRect:backgroundImagePosition];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.backgroundColor = [[UIColor alloc] initWithPatternImage:image];
I should do the job but does not work. 我应该做这份工作,但是不起作用。
Any idea what is wrong? 知道有什么问题吗? Thanks in advance.
提前致谢。
I'm still not able to replicate the stretching effect you're talking about, but I believe most of your problem lies in this line UIGraphicsBeginImageContext(backgroundImageSize);
我仍然无法复制您正在谈论的拉伸效果,但是我相信您的大部分问题都在这一行中
UIGraphicsBeginImageContext(backgroundImageSize);
. 。 What you want is for the new image to be the size of your view, not the size it already is.
您想要的是使新图像成为视图的大小,而不是现有的大小。 Anyway, this code works for me.
无论如何,这段代码对我有用。 Let me know if it works for you as well.
让我知道它是否也适用于您。
UIImage *backgroundImage = [UIImage imageNamed:@"analog_display_bgr.png"];
UIGraphicsBeginImageContext(self.bounds.size);
CGRect imagePosition = CGRectMake((self.bounds.size.width / 2) - (backgroundImage.size.width / 2),
(self.bounds.size.height / 2) - (backgroundImage.size.height / 2),
backgroundImage.size.width,
backgroundImage.size.height);
[backgroundImage drawInRect:imagePosition];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.backgroundColor = [[UIColor alloc] initWithPatternImage:image];
I tried your code, and I don't get anything at all. 我尝试了您的代码,却一无所获。 Completely blank view.
完全空白的视图。 The way I would do what you're describing is like this:
我将按照您的描述进行操作的方式如下:
UIImage *backgroundImage = [UIImage imageNamed:@"analog_display_bgr.png"];
UIImageView *imageView = [[UIImageView alloc]initWithImage:backgroundImage];
CGRect imageViewFrame = imageView.frame;
imageViewFrame.origin.x = (self.bounds.size.width / 2) - (imageView.bounds.size.width / 2);
imageViewFrame.origin.y = (self.bounds.size.height / 2) - (imageView.bounds.size.height / 2);
[imageView setFrame:imageViewFrame];
[self addSubview:imageView];
I tried it out in - (void)awakeFromNib
in a UIView subclass and it works fine. 我在UIView子类的
- (void)awakeFromNib
中进行了尝试,并且工作正常。 Draws the image in the center of the view without stretching it. 在视图中心绘制图像而不拉伸它。
If I understand you correctly you want the image to be the background of the view, so behind everything else. 如果我对您的理解正确,那么您希望图像成为视图的背景,那么在其他所有内容之后。 The code I provided above will do that, assuming you add the UIImageView behind all the other views.
假设您将UIImageView添加到所有其他视图的后面,我上面提供的代码将做到这一点。 If you still want the image to be drawn as the backgroundColor, like me know and I'll see if I can make it work.
如果您仍然希望将图像绘制为backgroundColor,就像我知道的那样,我会看看是否可以使它工作。
Sorry for posting yet another answer, but I was not able to add images to a comment, and I want to make sure I completely understand what you want. 很抱歉发布了另一个答案,但是我无法在评论中添加图片,因此我想确保自己完全了解您想要的内容。
So, from what I understand, this is what your code does 因此,据我了解,这就是您的代码所要做的
This is what my code does: 这是我的代码的作用:
But what you want is something more like this: 但是您想要的是更多这样的东西:
Is that correct? 那是对的吗? If yes, which one?
如果是,是哪一个? The one to the left or to the right?
一个在左边还是右边?
You can add UIImageView
to your UIView
and set the image to it. 您可以将
UIImageView
添加到您的UIView
并为其设置图像。 You can also play with UIViewContentMode
property, maybe UIViewContentModeCenter
or UIViewContentModeScaleAspectFill
with clipToBounds = YES
:) 您也可以使用
UIViewContentMode
属性,可能clipToBounds = YES
具有clipToBounds = YES
UIViewContentModeCenter
或UIViewContentModeScaleAspectFill
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.