Reputation: 135
I want to know how to save the drawing of my app like an image on the phone and then send it to a server.
here's my code.
I have this class where I do my drawing.
import Foundation
import UIKit
class Sign: UIViewController
{
@IBOutlet weak var btnSave: UIButton!
@IBOutlet weak var btnClear: UIButton!
let canvas = Canvas()
fileprivate func setupLayout() {
let stackView = UIStackView(arrangedSubviews: [btnClear,btnSave])
view.addSubview(stackView)
stackView.distribution = .fillEqually
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
}
override func viewDidLoad() {
view.addSubview(canvas)
canvas.backgroundColor = .white
canvas.frame = view.frame
setupLayout()
}
@IBAction func ClearCanvas(_ sender: Any) {
canvas.clear()
}
@IBAction func SaveCanvas(_ sender: Any) {
}
}
I have this Canvas class also where I have the functions of the buttons. Just want to know how to handle or convert my view with the drawing to an image and save it then send it to the server.
import Foundation
import UIKit
class Canvas: UIView {
func clear(){
lines.removeAll()
setNeedsDisplay()
}
override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else {return}
context.setStrokeColor(UIColor.black.cgColor)
context.setLineWidth(5)
context.setLineCap(.butt)
lines.forEach { (line) in
for (i , p) in line.enumerated(){
if i == 0 {
context.move(to: p)
} else {
context.addLine(to: p)
}
}
}
context.strokePath()
}
var lines = [[CGPoint]]()
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
lines.append([CGPoint]())
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let point = touches.first?.location(in: nil) else {return}
guard var lastline = lines.popLast() else { return }
lastline.append(point)
lines.append(lastline)
setNeedsDisplay()
}
}
Upvotes: 2
Views: 828
Reputation: 126
As you have drawn everything in the view named Canvas that why don't you just convert this view into an image like taking a screenshot.
Following is an extension of UIView having a computed property named screenshot which will provide you with the desired image:-
extension UIView {
var screenshot : UIImage? {
var screenshot :UIImage?
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
guard let context = UIGraphicsGetCurrentContext() else {return nil}
layer.render(in:context)
screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return screenshot
}
}
How to use:-
@IBAction func SaveCanvas(_ sender: Any) {
let drawing = canvas.screenshot
//Now send this image to server
}
Upvotes: 0
Reputation: 2160
You can add a function like in your class Canvas
to draw the image and update the data of image to your server.
func screenShot() -> UIImage? {
//Create the UIImage
UIGraphicsBeginImageContext(self.frame.size)
self.layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
Upvotes: 3