【问题标题】:how can i set Image directly on UIView?如何直接在 UIView 上设置图像?
【发布时间】:2011-10-19 05:36:33
【问题描述】:

谁能告诉我如何直接在 UIView 上设置 Image ? 这是我的代码:

 UIView *view=[[UIView alloc]init];
 [view setImage:[UIImage imageNamed:@"image.png"]];

【问题讨论】:

  • 你不想使用 UIImageView 吗?
  • 你需要使用 UIImageView
  • 可以设置为背景 aView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"yourimage.png"]];

标签: iphone uiimageview addsubview


【解决方案1】:
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
[view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"a.png"]]];

希望对你有所帮助。

【讨论】:

    【解决方案2】:

    这很简单。试试:

    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(origin_x, origin_y, width, height)]; 
    UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"a.png"]];
    [view addSubview:imageView];
    [imageView release];
    

    您还可以使用UIImageViewframe 属性在UIView 中移动它。希望有帮助!

    【讨论】:

      【解决方案3】:
         UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
              [imageView setImage:[UIImage imageNamed:@"320_480clouds_background.png"]];
              self.tableView.backgroundView = imageView;
              [imageView release];
      

      希望对你有帮助

      【讨论】:

        【解决方案4】:

        斯威夫特版本:

        let view = UIView(frame: CGRectMake(0, 0, 640, 200))
        view.backgroundColor = UIColor(patternImage: UIImage(named: "myImage")!)
        

        斯威夫特 3:

        let view = UIView(frame: CGRect(x: 0, y: 0, width: 640, height: 200))
        view.backgroundColor = UIColor(patternImage: UIImage(named: "myImage")!)
        

        【讨论】:

          【解决方案5】:

          你可以试试这个:

          UIView *aView=[[UIView alloc]init];
          
          UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"anImage.png"]];
          aView.backgroundColor = background;
          [background release];
          

          【讨论】:

          • 但为了获得良好的实践,您应该使用 UIImageView 来显示图像
          【解决方案6】:

          在这里,我将 CGImage 分配给接受 CGImage 或 NSImage 的图层的内容属性。您只能将 CGImage 或 NSImage 分配给 contents 属性。

          layerView.layer.contents = UIImage(named: "Mario.png")?.cgImage
          

          【讨论】:

          • 请提供一些解释,说明为什么您的代码解决了 OP 的问题,以及为什么它比已经给出的答案更好。这可能会有所帮助 - How to Answer
          • 我不会说每个人的答案都更好,但这只是您可以做到的另一种方式。
          • 即便如此,最好解释一下它的不同之处。
          • 我将 CGImage 分配给接受 CGImage 或 NSImage 的 layer 的 contents 属性。您只能将 CGImage 或 NSImage 分配给 contents 属性。
          • 这是我在 layerView 上使用 CALayer 的对象完成的,它是 UIKit 的低级对象。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-09-18
          • 1970-01-01
          • 1970-01-01
          • 2011-03-09
          • 1970-01-01
          相关资源
          最近更新 更多