Question

Can any body tell me how can I set Image directly on UIView ? Here is my code:

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

Solution

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

Hope this is help you.

OTHER TIPS

This is very simple to do. Try:

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

You can also play with the UIImageView's frame property to move it around in your UIView. Hope that Helps!

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

Hope this help you

Swift version :

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

Swift 3:

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

you can try this :

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

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

Here I am assigning the CGImage to the contents property of layer which accepts CGImage or NSImage. You can only assign CGImage or NSImage to contents property.

layerView.layer.contents = UIImage(named: "Mario.png")?.cgImage
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top