我怎么能直接在UIView上设置Image?

时间:2011-10-19 05:36:34

标签: iphone uiimageview addsubview

任何人都可以告诉我如何直接在UIView上设置图片? 这是我的代码:

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

6 个答案:

答案 0 :(得分:20)

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

希望这对你有帮助。

答案 1 :(得分:10)

这很简单。尝试:

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];

您还可以使用UIImageView框架属性进行游戏,以便在UIView中移动它。希望有帮助!

答案 2 :(得分:4)

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

希望这能帮到你

答案 3 :(得分:3)

Swift版本:

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")!)

答案 4 :(得分:2)

你可以试试这个:

UIView *aView=[[UIView alloc]init];

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

答案 5 :(得分:1)

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

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