質問編集履歴
1
コード全文追加しました!
test
CHANGED
File without changes
|
test
CHANGED
@@ -47,3 +47,165 @@
|
|
47
47
|
|
48
48
|
|
49
49
|
バージョンはxcodeの最新版を使っています。
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
###コード全文
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
import UIKit
|
58
|
+
|
59
|
+
import GPUImage
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
class CameraViewController: UIViewController {
|
64
|
+
|
65
|
+
//カメラの制御
|
66
|
+
|
67
|
+
fileprivate var mainCamera: GPUImageStillCamera?
|
68
|
+
|
69
|
+
//カメラのプレビューを表示する画面
|
70
|
+
|
71
|
+
fileprivate var previewView:GPUImageView?
|
72
|
+
|
73
|
+
//スクリーンのサイズを取得する
|
74
|
+
|
75
|
+
let mainScreenSize: CGSize = UIScreen.main.bounds.size
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
@IBOutlet weak var cameraPosChangeButton: UIButton!
|
80
|
+
|
81
|
+
override func viewDidLoad(){
|
82
|
+
|
83
|
+
super.viewDidLoad()
|
84
|
+
|
85
|
+
//カメラの初期化
|
86
|
+
|
87
|
+
cameraInit()
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
//シャッターボタンの配置
|
92
|
+
|
93
|
+
view.addSubview(shutterButton)
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
//ピンチジェスチャーの設定
|
98
|
+
|
99
|
+
let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(self.pinchedGesture(_ :)))
|
100
|
+
|
101
|
+
previewView?.addGestureRecognize(pinchGesture)
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
//カメラ切り替えボタンを手前に移動
|
106
|
+
|
107
|
+
self.view.bringSubviewToFront(cameraPosChangeButton)
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
//カメラの初期化
|
112
|
+
|
113
|
+
cameraInit()
|
114
|
+
|
115
|
+
view.addSubview(shutterButton)
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
///シャッターボタン
|
120
|
+
|
121
|
+
lazy var shutterButton:UIButton = {
|
122
|
+
|
123
|
+
let frame: CGRect = CGRect(x: 0, y: 0, width: 80, height: 80)
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
let button :UIButton = UIButton(frame: frame)
|
128
|
+
|
129
|
+
let image = UIImage(named: "img56741468")
|
130
|
+
|
131
|
+
button.setBackgroundImage(image, for: .normal)
|
132
|
+
|
133
|
+
button.center = CGPoint (x: mainScreenSize.width / 2, y: (previewView?.frame.maxY)! + (mainScreenSize.height - (previewView?.frame.maxY)! ) / 2)
|
134
|
+
|
135
|
+
button.addTarget(self, action: #selector(onShutterButtonClicked(sender:)),for: .touchUpInside)
|
136
|
+
|
137
|
+
return button
|
138
|
+
|
139
|
+
}()
|
140
|
+
|
141
|
+
@objc func onShutterButtonClicked(sender: AnyObject){
|
142
|
+
|
143
|
+
//撮影実行
|
144
|
+
|
145
|
+
capturePhotoProcess()
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
////カメラ撮影実行処理
|
150
|
+
|
151
|
+
func capturePhotoProcess(){
|
152
|
+
|
153
|
+
let cropFilter = GPUImageCropFilter(cropRegion : CGRect(x: 0, y: 0, width:1, height :1))
|
154
|
+
|
155
|
+
mainCamera?.addTarget(cropFilter)
|
156
|
+
|
157
|
+
mainCamera?.capturePhotoAsImageProcessedUp(toFilter: cropFilter, withCompletionHandler : { (image, error) in
|
158
|
+
|
159
|
+
UIImageWriteToSavedPhotosAlbum(image!, self, nil, nil)
|
160
|
+
|
161
|
+
})
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
//カメラを初期化する
|
166
|
+
|
167
|
+
func cameraInit(){
|
168
|
+
|
169
|
+
//カメラの設定をする
|
170
|
+
|
171
|
+
mainCamera = GPUImageStillCamera(sessionPreset: AVCaptureSession.Preset.photo.rawValue, cameraPosition: .back)
|
172
|
+
|
173
|
+
mainCamera!.outputImageOrientation = .portrait
|
174
|
+
|
175
|
+
previewView = GPUImageView(frame: CGRect(x: 0, y:0, width: mainScreenSize.width,height: (mainScreenSize.width / 3) * 4))
|
176
|
+
|
177
|
+
//プレビューとカメラを接続する
|
178
|
+
|
179
|
+
mainCamera?.addTarget(previewView!)
|
180
|
+
|
181
|
+
view.addSubview(previewView!)
|
182
|
+
|
183
|
+
//描写開始
|
184
|
+
|
185
|
+
mainCamera?.startCapture()
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
override var prefersStatusBarHidden: Bool{
|
190
|
+
|
191
|
+
return true
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
@IBAction func onCameraPosChange(_ sender: UIButton) {
|
198
|
+
|
199
|
+
//カメラの切り替えを行う
|
200
|
+
|
201
|
+
mainCamera?.rotateCamera()
|
202
|
+
|
203
|
+
//ミラー効果をONにする
|
204
|
+
|
205
|
+
mainCamera?.horizontallyMirrorFrontFacingCamera = true
|
206
|
+
|
207
|
+
}
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
}
|