I am developing a game which needs to render tiled pipe form the previously loaded cgImage. For this purpose I am using the following code, however the resized image is not rendered Can someone give me an advice how to fix that?
EDIT: to include minimal code for reproduction `
import SwiftUI
struct ContentView: View {
@State var reImage : UIImage?
var body: some View {
VStack {
Image("pipe-green")
.resizable()
.scaledToFit()
Image(uiImage: reImage!)
.resizable()
.scaledToFit()
Button("Resize") {
let sizeNew = CGSize(width: 100, height: 300)
reImage = resizeImage(textures: (pipe: "pipe-green", cap: "bottom-green"), of: sizeNew)
}
}
}
func resizeImage(textures: (pipe: String, cap: String), of size: CGSize) -> UIImage? {
guard let texture = UIImage(named: textures.pipe)?.cgImage else {
return nil
}
let textureRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
// Render tiled pipe form the previously loaded cgImage
UIGraphicsBeginImageContext(size)
let context = UIGraphicsGetCurrentContext()
context?.draw(texture, in: textureRect, byTiling: true)
let tiledBackground = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return tiledBackground
}
}