how can i set Image directly on UIView?
Asked Answered
S

6

18

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"]];
Sisile answered 19/10, 2011 at 5:36 Comment(3)
you don't want to use UIImageView ?Sharpe
you need to use a UIImageViewEcology
You can set it as background aView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"yourimage.png"]];Loaded
P
21
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
[view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"a.png"]]];

Hope this is help you.

Platonic answered 10/10, 2013 at 19:12 Comment(0)
C
11

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!

Confound answered 19/10, 2011 at 5:43 Comment(0)
C
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];

Hope this help you

Charley answered 19/10, 2011 at 6:0 Comment(0)
C
4

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")!)
Counterword answered 21/9, 2015 at 19:49 Comment(0)
S
2

you can try this :

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

UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"anImage.png"]];
aView.backgroundColor = background;
[background release];
Sharpe answered 19/10, 2011 at 5:45 Comment(1)
but for good practice you should use UIImageView for displaying imagesSharpe
I
2

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
Ignoble answered 6/2, 2017 at 11:17 Comment(5)
Please provide some explanation around why your code solves the OP's problem, and why it's better than the answers already given. This may help - How to AnswerSantasantacruz
I wouldn't say that it is better everyone's answer but it is just another way you can do it.Ignoble
Even so, an explanation of how it differs would be nice.Santasantacruz
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.Ignoble
This I have done using object of CALayer on the layerView which is low-level object of UIKit.Ignoble

© 2022 - 2024 — McMap. All rights reserved.