Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display a picture on UIView programmatically?

I want to ask a question about the UIview on the iPhone applicaiton. I want to display a image one the UIView programmatically, but I don't know how to do it. What should I do to display the image. Thank you very much.

like image 680
Questions Avatar asked Jul 30 '10 01:07

Questions


People also ask

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 render UIView to UIImage?

extension UIView { // Using a function since `var image` might conflict with an existing variable // (like on `UIImageView`) func asImage() -> UIImage { if #available(iOS 10.0, *) { let renderer = UIGraphicsImageRenderer(bounds: bounds) return renderer. image { rendererContext in layer. render(in: rendererContext.

How do you create a UIImage?

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


1 Answers

It's very easy just assign CGImageRef to view's layer's content, eg:

layerView.layer.contents = UIImage(named: "Mario.png")?.cgImage
like image 173
Anirudha Mahale Avatar answered Sep 22 '22 08:09

Anirudha Mahale