简体   繁体   English

在 Aspect Fit 上制作 UIView 的背景图像

[英]Making the Background Image of a UIView on Aspect Fit

I'm working on a sort of drawing app using objective-c, and for one of my UIView s, I want there to be a background image to it.我正在使用objective-c 开发一种绘图应用程序,对于我的一个UIView ,我希望它有一个背景图像。 However, I want this background image on the actual UIView , not a separate UIImageView .但是,我希望在实际的UIView上使用此背景图像,而不是单独的UIImageView I did that using this:我是用这个做的:

self.tempDrawImage.image = [UIImage imageNamed:@"image.jpg"];

In this code, tempDrawImage is a UIImageView I made programmatically, and after initializing it, I wrote this later in the code so that the drawings would appear on top of the image.在这段代码中,tempDrawImage 是我以编程方式制作的 UIImageView,在初始化它之后,我稍后在代码中编写了它,这样绘图就会出现在图像的顶部。 I don't know if this is helpful, but I thought I'd include it anyway just in case it does help.我不知道这是否有帮助,但我想无论如何我都会包括它,以防万一它确实有帮助。

- (UIImageView *)tempDrawImage
{
    if(!_tempDrawImage) _tempDrawImage = [UIImageView new];
    return _tempDrawImage;
}

- (void)drawRect:(CGRect)rect
{
    UIGraphicsGetCurrentContext();
    [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

Now, I'm struggling to make the image that I set to image.jpg in the beginning aspect fit.现在,我正在努力使我在开始方面设置为 image.jpg 的图像适合。 How could I do that?我怎么能那样做?

尝试这个

self.tempDrawImage.contentMode = UIViewContentModeScaleAspectFit;

您可以使用UIViewContentModeScaleAspectFit

 self.tempDrawImage.contentMode = UIViewContentModeScaleAspectFit;
UIGraphicsBeginImageContext(view.frame.size)
UIImage(named: "YOUR-IMAGE-NAME-HERE")!.drawInRect(view.frame)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsGetCurrentContext()
view.backgroundColor = UIColor(patternImage: image)

It works for me... 这个对我有用...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM