NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

How do I create UIImageView the same size as png dimensions?

Forums > iOS

I have the following code that creates a UIImageView to the same dimensions as a png image stored in the devices photos library:

func getImage(){
        let fileManager = FileManager.default
        let imagePath = (self.getDirectoryPath() as NSString).appendingPathComponent("\(word.components(separatedBy: "::")[0]).png")
        print("imagePath in getImage in DetailWord is: \(imagePath)")
        if fileManager.fileExists(atPath: imagePath){
            self.wordImage.image = UIImage(contentsOfFile: imagePath)
            save.isEnabled = false
            clear.isEnabled = true
            share.isEnabled = true
            wordImage.frame = .init(origin: CGPoint(x: (768 / 2) - (wordImage.image!.size.width / 2), y: (924 / 2) - (wordImage.image!.size.height / 2)), size: wordImage.image!.size)
            tempImage.frame = .init(origin: CGPoint(x: (768 / 2) - (wordImage.image!.size.width / 2), y: (924 / 2) - (wordImage.image!.size.height / 2)), size: wordImage.image!.size)
        } else {
            print("No Image")
            wordImage.image = nil
            save.isEnabled = false
            clear.isEnabled = false
            share.isEnabled = false

The code does the task of creating a UIImageView centered in the UIViewController, which exactly encapsulated the png image dimensions. However, when I tap on the png image or one of the coloured pencils (as you can see in the linked screen dumps) and draw on the png image, the image resizes itself (as in the second screen dump).

I can't seem to see anything about this when I use a search engine.

![] ![]


Hacking with Swift is sponsored by NSSpain

SPONSORED Announcing NSSpain 2020: Remote Edition! An online, continuous conference for iOS developers. We’ll start on Thursday and finish on Friday, with talks, activities, and lots of fun for 36 hours, non-stop. Sound good? Join us!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

Not logged in

Log in

Link copied to your pasteboard.