Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIImageview Programmatically in swift

I'm just trying to create a UIImage View programmatically, I have a new view and I tried doing this

let imageName = "yourImage.png"
view.backgroundColor = UIColor.colorWithPatternImage(UIImage(named:imageName))

This code doesn't work. please help me as soon as possible

like image 792
Laxman Ghimire Avatar asked Apr 05 '15 09:04

Laxman Ghimire


2 Answers

This is for image with 100 height and 100 width:

var imageViewObject :UIImageView
imageViewObject = UIImageView(frame:CGRectMake(0, 0, 100, 100));
imageViewObject.image = UIImage(named:"imageName.png")
self.view.addSubview(imageViewObject)

To resize the image to fit the view frame:

    imageViewObject.contentMode = UIViewContentMode.ScaleToFill
 or
    imageViewObject.contentMode = UIViewContentMode.ScaleAspectFit
or
    imageViewObject.contentMode = UIViewContentMode.ScaleAspectFill 
like image 165
Want Query Avatar answered Nov 15 '22 10:11

Want Query


First create UIImageView , add image in UIImageView with frame and than give image Name.The following code help you.

var imageView : UIImageView
imageView  = UIImageView(frame:CGRectMake(10, 50, 100, 300));
imageView.image = UIImage(named:"image.jpg")
self.view.addSubview(imageView)
like image 29
Bhuwan Sharma Avatar answered Nov 15 '22 11:11

Bhuwan Sharma