teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

修正

2019/12/02 04:42

投稿

MashisonJr.
MashisonJr.

スコア11

title CHANGED
File without changes
body CHANGED
@@ -1,7 +1,6 @@
1
1
  Swiftでライブラリの画像を選択したらその画像が複製される問題を解決したい
2
2
  複製されないようにするにはどうしたらいいのか?を知りたい
3
- ```
3
+ ```Swift
4
-
5
4
  import UIKit
6
5
 
7
6
  //画像を選択
@@ -131,4 +130,5 @@
131
130
 
132
131
  }
133
132
 
133
+ }
134
- }```
134
+ ```

2

修正

2019/12/02 04:41

投稿

MashisonJr.
MashisonJr.

スコア11

title CHANGED
File without changes
body CHANGED
@@ -1,6 +1,6 @@
1
1
  Swiftでライブラリの画像を選択したらその画像が複製される問題を解決したい
2
2
  複製されないようにするにはどうしたらいいのか?を知りたい
3
- ```Swift
3
+ ```
4
4
 
5
5
  import UIKit
6
6
 

1

修正

2019/12/02 04:40

投稿

MashisonJr.
MashisonJr.

スコア11

title CHANGED
File without changes
body CHANGED
@@ -1,2 +1,134 @@
1
1
  Swiftでライブラリの画像を選択したらその画像が複製される問題を解決したい
2
- 複製されないようにするにはどうしたらいいのか?を知りたい
2
+ 複製されないようにするにはどうしたらいいのか?を知りたい
3
+ ```Swift
4
+
5
+ import UIKit
6
+
7
+ //画像を選択
8
+ extension TapViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
9
+
10
+ //画像を選んだ時の処理
11
+ func imagePickerController(_ picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
12
+
13
+ let selectedImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage
14
+
15
+ self.imageView.image = image
16
+ UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
17
+ //サイズを圧縮する
18
+ // let resizedImage = selectedImage.scale(byFactor: 0.4)
19
+
20
+ image = selectedImage
21
+
22
+ var imageHeight = image.size.height
23
+ var imageWidth = image.size.width
24
+
25
+ let navigationBarHeight = navigationController?.navigationBar.frame.height
26
+ let width = self.view.frame.width
27
+ let height = self.view.frame.height
28
+ let centerX = self.view.center.x
29
+ let centerY = self.view.center.y
30
+ let widthRatio = imageWidth
31
+ let heightRatio = imageHeight
32
+ //画像の大きさに応じてiamgeviewのサイズを変える
33
+ if imageHeight > self.view.frame.height || imageWidth > self.view.frame.width {
34
+ print("1")
35
+ imageWidth = width
36
+ imageHeight = width*heightRatio/widthRatio
37
+
38
+ } else if imageHeight > self.view.frame.height {
39
+ print("2")
40
+ imageHeight = height
41
+ imageWidth = height*widthRatio/heightRatio
42
+
43
+ } else if imageWidth > self.view.frame.width {
44
+ print("3")
45
+ imageWidth = width
46
+ imageHeight = width*heightRatio/widthRatio
47
+
48
+ } else {
49
+ }
50
+
51
+ imageView.contentMode = UIView.ContentMode.scaleToFill
52
+ imageView.frame.size = CGSize(width: imageWidth, height: imageHeight)
53
+ //画像がnavigationbarに被らないようにする
54
+ if imageHeight/2 > (height/2 - navigationBarHeight!) {
55
+ print("4")
56
+ imageView.center = CGPoint(x: centerX, y: centerY + navigationBarHeight!)
57
+ } else {
58
+ print("5")
59
+ imageView.center = CGPoint(x: centerX, y: centerY)
60
+ }
61
+
62
+ imageView.image = image
63
+
64
+ picker.dismiss(animated: true, completion: nil)
65
+ }
66
+
67
+
68
+
69
+ // 撮影がキャンセルされた時に呼ばれる
70
+ func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
71
+ picker.dismiss(animated: true, completion: nil)
72
+ }
73
+
74
+ func tappedlibrary() {
75
+ let sourceType:UIImagePickerController.SourceType = UIImagePickerController.SourceType.photoLibrary
76
+
77
+ if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
78
+ // インスタンスの作成
79
+ let cameraPicker = UIImagePickerController()
80
+ cameraPicker.sourceType = sourceType
81
+ cameraPicker.delegate = self
82
+ self.present(cameraPicker, animated: true, completion: nil)
83
+ }
84
+ else{
85
+ print("error")
86
+
87
+ }
88
+ }
89
+
90
+ func tappedcamera() {
91
+ let sourceType:UIImagePickerController.SourceType = UIImagePickerController.SourceType.camera
92
+ // カメラが利用可能かチェック
93
+ if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera){
94
+ // インスタンスの作成
95
+ let cameraPicker = UIImagePickerController()
96
+ cameraPicker.sourceType = sourceType
97
+ cameraPicker.delegate = self
98
+ self.present(cameraPicker, animated: true, completion: nil)
99
+
100
+ }
101
+ else{
102
+ print("error")
103
+ }
104
+ }
105
+
106
+ @IBAction func selecteImageButton(_ sender: UITapGestureRecognizer) {
107
+
108
+ //アラート表示のために
109
+ let actionSheet = UIAlertController(title: "", message: "写真の選択", preferredStyle: UIAlertController.Style.actionSheet)
110
+
111
+ let tappedcamera = UIAlertAction(title: "カメラで撮影", style: UIAlertAction.Style.default, handler: {
112
+ (action: UIAlertAction!) in
113
+ self.tappedcamera()
114
+ })
115
+
116
+ let tappedlibrary = UIAlertAction(title: "ライブラリから選択", style: UIAlertAction.Style.default, handler: {
117
+ (action: UIAlertAction!) in
118
+ self.tappedlibrary()
119
+ })
120
+
121
+ let cancel = UIAlertAction(title: "キャンセル", style: UIAlertAction.Style.cancel, handler: {
122
+ (action: UIAlertAction!) in
123
+ print("キャンセル")
124
+ })
125
+
126
+ actionSheet.addAction(tappedcamera)
127
+ actionSheet.addAction(tappedlibrary)
128
+ actionSheet.addAction(cancel)
129
+
130
+ present(actionSheet, animated: true, completion: nil)
131
+
132
+ }
133
+
134
+ }```