UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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)
            self.view.addSubview(tempImage)
            self.view.addSubview(wordImage)
        } 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.

![https://www.dropbox.com/s/p3ma98nswra1y4h/IMG_0013.png?dl=0] ![https://www.dropbox.com/s/407oqqhk29dmojr/IMG_0014.png?dl=0]

3      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.