Swift Display Image UIimageview

How do you create a UIImage 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!)

Finally you'll need to give imageView a frame and add it your view for it to be visible:

imageView.frame = CGRect(x: 0, y: 0, width: 100, height: 200)
view.addSubview(imageView)

How to set an image in imageview swift 3?

Updated for Swift 3:

use below simple code, to set the image to UIImageView;

class YourViewControllerName: UIViewController {

var mYourImageViewOutlet: UIImageView?

func addImageToUIImageView{
var yourImage: UIImage = UIImage(named: "Birthday_logo")!
mYourImageViewOutlet.image = yourImage
} // call this function where you want to set image.
}

Note: "Birthday_logo" type of image must be present in your Assets.xcassets of your project.
I attached the screenshot if you want any help please refer it.

****// used anywhere you want to add an image to UIImageView. [Here I used one function & in that function, I write a code to set image to UIImageView]****

Enjoy..!Sample Image

How to display image in UIImageView like original image?

From your images, it appears that you have set the height of your UIImageView to be 110 points. Instead, you need the height to grow as the width grows.

Pin the top, leading, and trailing edges of your UIImageView to its superview. Finally, set the height of your UIImageView by creating an Aspect Ratio constraint that sets the image.width Equal To image.height with a multiplier of 320:110. You can do this by control-clicking in your UIImageView and dragging up and to the right and then releasing while still inside of your image view. Then choose Aspect Ratio from the pop-up. In the Attributes Inspector change the multiplier for the constraint to 320:110.

Aspect Ratio constraint in Attributes Inspector

As you indicated in your question, set the Content Mode of your UIImageView to Scale To Fill.

How to add image to UIImageView?

Simply:

if let image = img {
// you don't have to instantiate an imageView again, IBOutlet handles it for you
testImg.image = image
print("Works")
// if you don't see this output in your debug console, the condition never succeeds and an img is really nil
}

How do you display an image from fire storage to UIImageView on user profile View Controller?

I solved my own question so I figured I would post the solution. The UIImagePickerController code as listed in the question was correct and I am able to use this with one change to make the image load a lot faster. In place of the imageSelected = image?.pngData() I replaced that with this...

guard let imageSelected = image?.jpegData(compressionQuality: 0.5) else { return }

and I also created a function to download the image and placing it inside of the UIImageView each time a user revisits the profile page and if they choose they can simply click on the image and upload a new profile pic and the download images function keeps it set. Here is the function I used to accomplish this...

private func downloadimages() {
let reference = Storage.storage().reference(withPath: "\("images/"+Auth.auth().currentUser!.uid+"+.jpg")")
reference.downloadURL { (URL, error) in
let data = NSData(contentsOf: URL!)
let image = UIImage(data: data! as Data)
self.profileimage.image = image
}
}

I simply called this function in the

override func viewDidLoad() {
super.viewDidLoad()
}

and presto, works perfect!

Can't add UIImage to UIImageView (Swift)

Temporarily change

UIImage(named: "DiscoverButtonIcon.png")

To

UIImage(named: "DiscoverButtonIcon.png")!

Run the app. If it crashes, you have the wrong name or the image is not in your app bundle at all.

is saved in the iCloud Drive in the same directory as the Swift file

That sounds like the problem. That is not where the image needs to be. It needs to be in your asset catalog or app bundle.

UIImage and UIImageView show/hide problems

OK - you said in the comments that "The shutter button was added via code", so presumably your code looks something like this:

class ViewController: UIViewController {

let shutterButton = UIButton()

override func viewDidLoad() {
super.viewDidLoad()

// other stuff

view.addSubview(shutterButton)

// constraints for shutterButton and other elements

}

}

then, when you get the photo image, you're doing this:

    let image = UIImage(data: data)

let Photo = UIImageView(image: image)

if shutterButton.tag == 0 {
shutterButton.tag = 1
Photo.contentMode = .scaleAspectFill
view.insertSubview(Photo, belowSubview: shutterButton)
}
else if shutterButton.tag == 1
{
shutterButton.tag = 0
}

which means you're creating a NEW image view and inserting it as a subview every time this is called.

If you want to "add a photo" / "remove the photo" on every-other button tap, you want to have your image view already there, so you can set the image and show or hide the image view instead of creating new ones.

So, add your image view the same way you added the button... but add it before you add the button (so the button will be "on top"), and set it to hidden to start with:

class ViewController: UIViewController {

let shutterButton = UIButton()

let photoImageView = UIImageView()

override func viewDidLoad() {
super.viewDidLoad()

// other stuff

// setup the photo image view
photoImageView.contentMode = .scaleAspectFill

// start with it hidden
photoImageView.isHidden = true

// add the photo image view to the view hierarchy
view.addSubview(photoImageView)

// add the button last
view.addSubview(shutterButton)

// constraints for the image view

// constraints for shutterButton and other elements

}

}

then, when you handle the photo capture:

extension ViewController: AVCapturePhotoCaptureDelegate {
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto
photo: AVCapturePhoto, error: Error?) {

guard let data = photo.fileDataRepresentation() else {
return
}

let image = UIImage(data: data)

// don't do this
//let Photo = UIImageView(image: image)

if shutterButton.tag == 0 {
shutterButton.tag = 1

// don't do this
//Photo.contentMode = .scaleAspectFill
//view.insertSubview(Photo, belowSubview: shutterButton)

// instead, do this
photoImageView.image = image
photoImageView.isHidden = false

}
else if shutterButton.tag == 1
{
shutterButton.tag = 0

// do this to "remove" the image
photoImageView.image = nil
photoImageView.isHidden = true
}
}
}

how to programmatically, in Swift, set an uiimageview to the xcode built-in applewatch icon image?

You seem to be talking about the SF symbol applewatch. You can get that (or any other SF symbol) by using UIImage(systemName:):

UIImage(systemName: "applewatch")

Notes:

  • The color of the watch depends on the tintColor of the image view
  • Available on iOS 14+


Related Topics



Leave a reply



Submit