5

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

2
  • 1
    possible duplicate of How do you create a UIImage View Programmatically - Swift Commented Apr 5, 2015 at 9:16
  • Hi, we want to help you, but with "This code doesn't work" we don't have enough information. How does it not work? What happens? Also, writing "please help me as soon as possible" won't elicit answers any sooner... Commented Apr 5, 2015 at 9:16

2 Answers 2

14

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 
Sign up to request clarification or add additional context in comments.

Comments

4

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)

1 Comment

according question its true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.