I have a UIImageView that will serve as a drawing canvas and i want to implement a function that will get the content inside of UIImageView and convert it into a cgContect so it could be set as a background for the canvas to draw over but i ran into an issue. I have a reset button that will set the UIImageView.image to nil (to clear the current UIview) for that I tried to save the UIImageView into a canvasArray array to save it into a second view savedImageView
this is my reset function that will delete the content of mainImageView
@objc func resetPressed(_ sender: Any) {
mainImageView.image = nil
}
The array that will is store and object that receives mainImageView as a param and the function that saved it
var canvasArray: [Canvas] = []
@objc func saveCanvas(_ sender: Any) {.
//here is the UIImageView
guard let canvas = Canvas(name: "", canvas: mainImageView, numOfPages: 0) else {
return
}
canvasArray.append(canvas)
if canvasArray.count > 0 {
canvasArray.forEach{ canvas in
print("\(canvas.canvas.image)")
}
}
}
And the function that will get the iUIImageView.image and will convert it into cgContext
@objc func changeImagefunc(_ sender: Any) {
if canvasArray.count < pages {
return
}
if let savedImage = canvasArray[pages].canvas.image {
UIGraphicsBeginImageContext(view.frame.size)
savedImage.draw(in: view.bounds)
if let drawnImage = UIGraphicsGetImageFromCurrentImageContext() {
mainImageView.image = drawnImage
}
UIGraphicsEndImageContext()
print("clicked button ")
}
}
I have 3 different UIViewImage that will ser a specific purpose mainImageView and tempImageView that will check the start and and of the drawing (function(which works fine) and the savedImageView that will receive the image converted into "cgContext " . Three of them stored in a function that then gets passed into the viewDidLoad section
func setupImageViews() {
view.addSubview(tempImageView)
tempImageView.frame = view.bounds
if savedImageView.image != nil {
view.addSubview(savedImageView)
savedImageView.frame = view.bounds
} else {
view.addSubview(mainImageView)
mainImageView.frame = view.bounds
}
}
Like I said i can not get this to work because the resetButton/function is deleted not only the current UIImageView (which is what is intended to do ) but also deletes the content inside the canvasArray which I want to avoid. Would there ve a different approach to it or what else do you recommend?