swift
1import UIKit 2 3class FitViewController: UIViewController { 4 5 //FirstViewから渡されるImageを受け取る 6 var receivedImage: UIImage? = nil 7 8 9 @IBOutlet weak var imageView: UIImageView! 10 var lastPoint = CGPoint.zero 11 var swiped = false 12 13 override func viewDidLoad() { 14 super.viewDidLoad() 15 // Do any additional setup after loading the view. 16 imageView.image = receivedImage 17 } 18 19 override func didReceiveMemoryWarning() { 20 super.didReceiveMemoryWarning() 21 // Dispose of any resources that can be recreated. 22 } 23 24 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 25 swiped = false 26 if let touch = touches.first{ 27 lastPoint = touch.location(in: self.view) 28 } 29 } 30 31 func drawLines(fromPoint:CGPoint,toPoint:CGPoint) { 32 UIGraphicsBeginImageContext(self.view.frame.size) 33 imageView.image?.draw(in: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)) 34 let context = UIGraphicsGetCurrentContext() 35 context?.move(to: CGPoint(x: fromPoint.x, y: fromPoint.y)) 36 context?.addLine(to: CGPoint(x: toPoint.x, y: toPoint.y)) 37 38 context?.setBlendMode(CGBlendMode.normal) 39 context?.setLineCap(CGLineCap.round) 40 context?.setLineWidth(10) 41 context?.setStrokeColor(UIColor(red: 0, green: 0, blue: 0, alpha: 1.0).cgColor) 42 43 context?.strokePath() 44 45 imageView.image = UIGraphicsGetImageFromCurrentImageContext() 46 UIGraphicsEndImageContext() 47 } 48 49 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 50 swiped = true 51 if let touch = touches.first { 52 let currentPoint = touch.location(in: self.view) 53 drawLines(fromPoint: lastPoint, toPoint: currentPoint) 54 55 lastPoint = currentPoint 56 } 57 } 58 59 override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 60 if !swiped { 61 drawLines(fromPoint: lastPoint, toPoint: lastPoint) 62 } 63 } 64 65 66 /* 67 // MARK: - Navigation 68 69 // In a storyboard-based application, you will often want to do a little preparation before navigation 70 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 71 // Get the new view controller using segue.destinationViewController. 72 // Pass the selected object to the new view controller. 73 } 74 */ 75 76}
現在、このように画像上に黒でお絵かきすることのできるプログラムまではかけるようになったのですが、黒で書く代わりに画像を消す操作をすることはできないのでしょうか?
https://teratail.com/questions/57171
この辺が参考になるかなと思ってみてるのですが、これは画像からcontextの形を消すのではなく、contextから長方形を消すメソッドのようです...。
なにか参考になる資料などもあればぜひ教えていただけるとうれしいです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/28 00:32
2017/03/28 00:47
2017/03/28 00:55