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

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

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

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

1672閲覧

(iOS)画像の一部を消したい

bruteberry

総合スコア20

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2017/03/28 00:06

編集2017/03/28 00:40

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から長方形を消すメソッドのようです...。

なにか参考になる資料などもあればぜひ教えていただけるとうれしいです。

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

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

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

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

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

guest

回答1

0

ベストアンサー

「消す」の定義が曖昧ですが、背景色で描けばいいのでは?
通常のペイントツールってそうなってませんかね。

「消す」

swift

1context?.setBlendMode(.clear)

これでどうでしょう?(色指定は不要です)

投稿2017/03/28 00:26

編集2017/03/28 00:46
fuzzball

総合スコア16731

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

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

bruteberry

2017/03/28 00:32

背景色で描きたくない理由があります。お絵かきアプリを作っているわけではないので...。 背景の画像を出したいのです。 透明化する方法はないのでしょうか?
fuzzball

2017/03/28 00:47

回答に追記しました。
bruteberry

2017/03/28 00:55

すごい!いつもいつもありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問