質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.51%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Q&A

1回答

687閲覧

Xcode8でimageViewを画像保存したいです

h.atsuki

総合スコア8

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

0グッド

0クリップ

投稿2017/08/20 07:06

教えてください!
butttonを押すと、imageViewを画像保存したいです。
その場合の手順及びソースコードを教えてほしいです。

宜しくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

以下のコードで指定のURLにPNG形式で画像を保存します。

swift

1func saveImageView(_ imageView: UIImageView, _ url: URL) { 2 guard let image = imageView.image else { return } 3 let pngData = UIImagePNGRepresentation(image) 4 try! pngData?.write(to: url) 5}

投稿2017/08/21 12:35

Stripe

総合スコア2183

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

h.atsuki

2017/08/22 06:39

import 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.atsuki

2017/08/22 06:43

ご回答ありがとうございます。 これが全体像です。 ちなみに入れてみましたが、errorが出てうまく動きませんでした、、 // savebuttonを押すとimageViewを保存 上記、buttonを押すとその時にimageViewに移っているものを画像保存したいです。 何卒、宜しくお願い致します。
Stripe

2017/08/22 11:34

それは本当に全体像なんですか? とりあえず、画像を保存するコードが無いようですが? また、エラーって、どんなエラーですか?
h.atsuki

2017/08/22 11:58

import 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.atsuki

2017/08/22 12:00

こちらが全体像です。 errorはなくなったのですが、Stripeさんに仰って頂いたコードの追記する箇所ってあっておりますでしょうか? 初心者でして、変な質問しておりましたらすみません。
Stripe

2017/08/22 14:43

とりあえず、Swift言語の勉強をしましょう。 それが分からないと、根本的にプログラミングができません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.51%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問