Trying to resize a UIViewRepresentable of a UIImage in swiftUI

Viewed 1269

I've got my with the resize method:

//
//  ImageView.swift
//  fairytales
//
//  Created by Justin Zaun on 7/12/20.
//  Copyright © 2020 JGZ. All rights reserved.
//

import SwiftUI

struct ImageView: UIViewRepresentable {
    
    var name: String
    
    fileprivate var imageView: UIImageView = UIImageView()
    fileprivate var originalImage: UIImage
    

    init(name: String) {
        self.name = name
        self.originalImage = UIImage(named: name)!
    }
    
    func makeUIView(context: Context) -> UIImageView {
        imageView.image = self.originalImage

        return imageView;
    }
    
    func updateUIView(_ uiView: UIImageView, context: Context) {
    }
    
    fileprivate func scaledImage(width: CGFloat, height: CGFloat) -> UIImage {
        let size = CGSize(width: width, height: height)
        if (self.originalImage.size == size) {
            return self.originalImage
        }
        
        UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
        self.originalImage.draw(in: CGRect(x: 0, y: 0, width: width, height: height))
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return image!;
    }
    
    func resize(width: CGFloat, height: CGFloat) -> some View {
        self.imageView.image = scaledImage(width: width, height: height)
        print(self.imageView.image!.size)

        return self.frame(width: width, height: height)
    }
}

Inside a view I try...

ImageView(name: "Letter-T")
    .resize(width: 100, height: 100)

The print statement, prints the correct size, but onscreen the image is not resized. What am I doing wrong?

0 Answers
Related