【问题标题】:iOS UIView Background ImageiOS UIView 背景图片
【发布时间】:2012-04-17 04:11:54
【问题描述】:

我想将UIImage作为UIView.上的背景图片

这是我的代码:

UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"login.png"]];
self.view.backgroundColor = background;
[background release];

问题是,规模不合适。它从 640 缩放到 480,所以我不能说它 100% 肯定,但它看起来只显示了 300 到 250 或类似的东西。

是否有适合缩放/适合 UIView/适合大小的模式?

【问题讨论】:

  • 原始 PNG 的大小是多少?你也是在 Retina 设备上查看这个吗?

标签: ios uiview


【解决方案1】:

你好,试试这个,

     self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"login.png"]];

【讨论】:

  • 最佳答案,清晰且立即生效。非常适合快速研究。
【解决方案2】:

根据UIColor API:

您可以使用图案颜色来设置填充或描边颜色,就像设置纯色一样。在绘制过程中,图案颜色的图像会根据需要平铺以覆盖给定区域。

这意味着它会尝试从您的图像中创建一个图案来填充该区域。我认为这样做的最佳方法是重新缩放您的图像以完全适合 UIView 大小。

这里有一个很好的答案,如何在运行时调整图像大小:

The simplest way to resize an UIImage?

【讨论】:

    【解决方案3】:

    指定一个实际的背景并将其发送到视图的后面

    //add background
    UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg.png"]];
    [myView addSubview:background];
    [myView sendSubviewToBack:background];
    myView.contentMode = UIViewContentModeScaleAspectFit;
    

    【讨论】:

      【解决方案4】:

      您需要在添加图像之前对其进行处理,试试这个:

      UIGraphicsBeginImageContext(self.view.frame.size);
      [[UIImage imageNamed:@"image.png"] drawInRect:self.view.bounds];
      UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      
      self.view.backgroundColor = [UIColor colorWithPatternImage:image];
      

      【讨论】:

      • 如果你把图像翻转了,你必须用 CGContextTranslateCTM() 改变上下文方向
      【解决方案5】:
      UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"image.png"]];
      self.view.backgroundColor = background;
      [background release];
      

      【讨论】:

        【解决方案6】:

        在 Interface Builder 中,将 Image View 添加到 View 并选择要在 Attributes inspector 中使用的图像。最后,通过拖动Document Outline 中的元素,使Image View 成为View 的第一个子元素。

        【讨论】:

          【解决方案7】:

          如果视图将具有相同的大小,您可以在 AppDelegate.m 中放置一个方法,在第一次运行时缩放背景图像,然后将缩放后的图像保存在内存中以供将来使用:

          UIImage *backgroundImage = nil;
          - (void)setBackground:(UIView *)view
          {
              if (backgroundImage == nil)
              {
                  UIGraphicsBeginImageContext(view.frame.size);
                  [[UIImage imageNamed:@"Background"] drawInRect:view.bounds];
                  backgroundImage = UIGraphicsGetImageFromCurrentImageContext();
                  UIGraphicsEndImageContext();
              }
              view.backgroundColor = [UIColor colorWithPatternImage:backgroundImage];
          }
          

          感谢@uneakharsh 的帮助!

          【讨论】:

            【解决方案8】:

            如果你想快速完成:

            self.view.backgroundColor = UIColor(patternImage: UIImage(named:"background.jpg"))
            

            【讨论】:

            • 是的,我想用 Swift 来做:D
            【解决方案9】:
            this.View.BackgroundColor = UIColor.FromPatternImage (UIImage.FromFile ("body_background.png"));
            

            【讨论】:

              【解决方案10】:

              您可以使用 UIGraphicsBeginImageContext 方法将图像的大小设置为与视图相同。

              语法:void UIGraphicsBeginImageContext(CGSize size);

                #define IMAGE(imageName) (UIImage *)[UIImage imageWithContentsOfFile:
              [[NSBundle mainBundle] pathForResource:imageName ofType:IMAGE_TYPE_PNG]]
              
                      UIGraphicsBeginImageContext(self.view.frame.size);
                      [[UIImage imageNamed:@“MyImage.png"] drawInRect:self.view.bounds];
                      UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
                      UIGraphicsEndImageContext();
              
                  self.view.backgroundColor = [UIColor colorWithPatternImage:IMAGE(@"mainBg")];
              

              【讨论】:

                【解决方案11】:

                Swift 中使用这个...

                    UIGraphicsBeginImageContext(self.view.frame.size)
                    UIImage(named: "1.png")?.drawInRect(self.view.bounds)
                
                    var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()
                
                    UIGraphicsEndImageContext()
                
                    self.view.backgroundColor = UIColor(patternImage: image)
                

                【讨论】:

                  【解决方案12】:

                  我在 Swift 中使用以下扩展:

                  extension UIView{
                  
                      func setBackgroundImage(img: UIImage){
                  
                          UIGraphicsBeginImageContext(self.frame.size)
                          img.drawInRect(self.bounds)
                          let patternImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
                          UIGraphicsEndImageContext()
                          self.backgroundColor = UIColor(patternImage: patternImage)
                      }
                  }
                  

                  在代码中你可以这样使用:

                  if let image = UIImage(named: "HomeBg"){
                          self.view.setBackgroundImage(image)
                      }
                  

                  【讨论】:

                    【解决方案13】:

                    我也想这样做,但我发现,由于 autolayoutUIImageView 会延伸包含 UIView,而当我想要它时:我想要 UIImageViewUIView 的大小。

                    所以我创建了这个类。每当通过layoutSubviews 布局时,它都会根据UIView 容器更新UIImageView 框架。

                    /**
                     * View with an image whose frame is adjusted to the frame of the view. 
                     */
                    class ViewWithImage: UIView {
                    
                        let image: UIImageView
                    
                        init(image: UIImageView) {
                            self.image = image
                            super.init(frame: .zero)
                            self.addSubview(image)
                        }
                    
                        override func layoutSubviews() {
                            super.layoutSubviews()
                            updateImageFrame()
                        }
                    
                        private func updateImageFrame() {
                            image.frame = CGRect(origin: .zero, size: self.frame.size)
                        }
                    
                        required init?(coder: NSCoder) { fatalError("no init(coder:)") }
                    }
                    

                    【讨论】: