h.atsuki2017/08/22 06:39import UIKit class ViewController: UIViewController { @IBAction func resetButton(_ sender: UIButton) { imageView.removeFromSuperview() } @IBAction func savebutton(_ sender: UIButton) { // savebuttonを押すとimageViewを保存 print("image") } @IBOutlet weak var imageView: UIImageView! var lastPoint = CGPoint.zero var currentPoint = CGPoint.zero var midPoint1 = CGPoint.zero var midPoint2 = CGPoint.zero var swiped = false override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } //直線の描画 func drawLines(fromPoint:CGPoint,toPoint:CGPoint){ UIGraphicsBeginImageContext(self.view.frame.size) imageView.image?.draw(in: CGRect(x:0,y:0,width:self.view.frame.width,height:self.view.frame.height)) let context = UIGraphicsGetCurrentContext() context?.move(to:CGPoint(x:fromPoint.x,y:fromPoint.y)) context?.addLine(to:CGPoint(x:toPoint.x,y:toPoint.y)) context?.setBlendMode(CGBlendMode.normal) context?.setLineCap(CGLineCap.round) context?.setLineWidth(1) context?.setStrokeColor(UIColor(red:0,green:0,blue:0,alpha:1).cgColor) context?.strokePath() imageView.image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() } //曲線の描画 func drawCurves(fromPoint:CGPoint,toPoint:CGPoint,controlPoint:CGPoint){ UIGraphicsBeginImageContext(self.view.frame.size) imageView.image?.draw(in: CGRect(x:0,y:0,width:self.view.frame.width,height:self.view.frame.height)) let context = UIGraphicsGetCurrentContext() context?.move(to:CGPoint(x:fromPoint.x,y:fromPoint.y)) context?.addQuadCurve(to:CGPoint(x:toPoint.x,y:toPoint.y),control:CGPoint(x:controlPoint.x,y:controlPoint.y)) context?.setBlendMode(CGBlendMode.normal) context?.setLineCap(CGLineCap.round) context?.setLineWidth(5) context?.setStrokeColor(UIColor(red:0,green:0,blue:0,alpha:1).cgColor) context?.strokePath() imageView.image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() } //タッチスタート override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.first{ lastPoint = touch.location(in: self.view) } } //タッチ移動 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.first{ //初回の移動なら最初の中間点まで直線を引く if swiped == false{ currentPoint = touch.location(in: self.view) midPoint1 = CGPoint(x:(lastPoint.x + currentPoint.x)/2,y:(lastPoint.y + currentPoint.y)/2) drawLines(fromPoint:lastPoint,toPoint:midPoint1) lastPoint = currentPoint swiped = true }else{ //2回目以降の移動なら中間点を繋いで曲線を引く currentPoint = touch.location(in: self.view) midPoint2 = CGPoint(x:(lastPoint.x + currentPoint.x)/2,y:(lastPoint.y + currentPoint.y)/2) drawCurves(fromPoint:midPoint1,toPoint:midPoint2,controlPoint:lastPoint) midPoint1 = midPoint2 lastPoint = currentPoint swiped = true } } } //最後のタッチは、最後の中間点から最終点まで直線を引く override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.first{ currentPoint = touch.location(in: self.view) drawLines(fromPoint: midPoint1, toPoint: currentPoint) swiped = false } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
h.atsuki2017/08/22 06:43ご回答ありがとうございます。 これが全体像です。 ちなみに入れてみましたが、errorが出てうまく動きませんでした、、 // savebuttonを押すとimageViewを保存 上記、buttonを押すとその時にimageViewに移っているものを画像保存したいです。 何卒、宜しくお願い致します。
h.atsuki2017/08/22 11:58import UIKit class ViewController: UIViewController { @IBAction func resetButton(_ sender: UIButton) { imageView.removeFromSuperview() } @IBAction func savebutton(_ sender: UIButton) { // savebuttonを押すとimageViewを保存 func saveImageView(_ imageView: UIImageView, _ url: URL) { guard let image = imageView.image else { return } let pngData = UIImagePNGRepresentation(image) try! pngData?.write(to: url) } } @IBOutlet weak var imageView: UIImageView! var lastPoint = CGPoint.zero var currentPoint = CGPoint.zero var midPoint1 = CGPoint.zero var midPoint2 = CGPoint.zero var swiped = false override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } //直線の描画 func drawLines(fromPoint:CGPoint,toPoint:CGPoint){ UIGraphicsBeginImageContext(self.view.frame.size) imageView.image?.draw(in: CGRect(x:0,y:0,width:self.view.frame.width,height:self.view.frame.height)) let context = UIGraphicsGetCurrentContext() context?.move(to:CGPoint(x:fromPoint.x,y:fromPoint.y)) context?.addLine(to:CGPoint(x:toPoint.x,y:toPoint.y)) context?.setBlendMode(CGBlendMode.normal) context?.setLineCap(CGLineCap.round) context?.setLineWidth(1) context?.setStrokeColor(UIColor(red:0,green:0,blue:0,alpha:1).cgColor) context?.strokePath() imageView.image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() } //曲線の描画 func drawCurves(fromPoint:CGPoint,toPoint:CGPoint,controlPoint:CGPoint){ UIGraphicsBeginImageContext(self.view.frame.size) imageView.image?.draw(in: CGRect(x:0,y:0,width:self.view.frame.width,height:self.view.frame.height)) let context = UIGraphicsGetCurrentContext() context?.move(to:CGPoint(x:fromPoint.x,y:fromPoint.y)) context?.addQuadCurve(to:CGPoint(x:toPoint.x,y:toPoint.y),control:CGPoint(x:controlPoint.x,y:controlPoint.y)) context?.setBlendMode(CGBlendMode.normal) context?.setLineCap(CGLineCap.round) context?.setLineWidth(5) context?.setStrokeColor(UIColor(red:0,green:0,blue:0,alpha:1).cgColor) context?.strokePath() imageView.image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() } //タッチスタート override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.first{ lastPoint = touch.location(in: self.view) } } //タッチ移動 override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.first{ //初回の移動なら最初の中間点まで直線を引く if swiped == false{ currentPoint = touch.location(in: self.view) midPoint1 = CGPoint(x:(lastPoint.x + currentPoint.x)/2,y:(lastPoint.y + currentPoint.y)/2) drawLines(fromPoint:lastPoint,toPoint:midPoint1) lastPoint = currentPoint swiped = true }else{ //2回目以降の移動なら中間点を繋いで曲線を引く currentPoint = touch.location(in: self.view) midPoint2 = CGPoint(x:(lastPoint.x + currentPoint.x)/2,y:(lastPoint.y + currentPoint.y)/2) drawCurves(fromPoint:midPoint1,toPoint:midPoint2,controlPoint:lastPoint) midPoint1 = midPoint2 lastPoint = currentPoint swiped = true } } } //最後のタッチは、最後の中間点から最終点まで直線を引く override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { if let touch = touches.first{ currentPoint = touch.location(in: self.view) drawLines(fromPoint: midPoint1, toPoint: currentPoint) swiped = false } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
h.atsuki2017/08/22 12:00こちらが全体像です。 errorはなくなったのですが、Stripeさんに仰って頂いたコードの追記する箇所ってあっておりますでしょうか? 初心者でして、変な質問しておりましたらすみません。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/22 06:39
2017/08/22 06:43
2017/08/22 11:34
2017/08/22 11:58
2017/08/22 12:00
2017/08/22 14:43