Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add image to view programmatically?

Tags:

uiimage

Say you have a UIImage *image and a UIView *v

How would you display the image on top of the view programmatically?

like image 259
James Raitsev Avatar asked Nov 11 '11 23:11

James Raitsev


People also ask

How to add image in view Programmatically Swift?

First you create a UIImage from your image file, then create a UIImageView from that: let imageName = "yourImage. png" let image = UIImage(named: imageName) let imageView = UIImageView(image: image!) Save this answer.

How do I add an image to UIImageView?

The first step is to drag the UIImageView onto your view. Then open the UIImageView properties pane and select the image asset (assuming you have some images in your project). You can also configure how the underlying image is scaled to fit inside the UIImageView.

How do I add an image to Xcode?

Create a new image set To create an image set, generate an image asset outside of Xcode, then import it into an asset catalog. The system converts the image format into the most appropriate representation when you build the project. In the Project navigator, select an asset catalog: a file with a .

How to declare image view Swift?

To create a new UIImage programmatically in Swift, we simply need to create a new instance of UIImage providing it with the name of the image we have added to a Resources folder. Once we have an instance of UIImage created, we can add it to a UIImageView.


1 Answers

If you just want to add the UIImage to the UIView then you need an UIImageView inbetween the UIView and UIImage, such as:

UIImage *image = [[UIImage alloc] init];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[iv setImage:image];
[v addSubview:iv];

Note that the above is just dummy code and I've created all UI elements with a zero frame.

like image 129
Peter Brooks Avatar answered Oct 06 '22 21:10

Peter Brooks