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

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

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

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

Q&A

1回答

1450閲覧

シャッターボタンを押しても、カメラ撮影されない

huyuhimituhiro

総合スコア7

Swift

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

0グッド

1クリップ

投稿2018/01/18 01:29

###前提・実現したいこと
アップルさんのサンプルコードに、位置情報を記録するコードを追加したのですが、コンパイルは通りますが、シャッターボタンを押しても撮影ができません

###発生している問題・エラーメッセージ

エラーメッセージ

###該当のソースコード

import UIKit import CoreLocation import AVFoundation import ImageIO import Photos class CameraViewController: UIViewController, CLLocationManagerDelegate, AVCaptureFileOutputRecordingDelegate { var locationManager = CLLocationManager() override func viewDidLoad() { super.viewDidLoad() // アプリ利用中の位置情報の利用許可を得る locationManager.requestWhenInUseAuthorization() // ロケーションマネージャのdelegeteになる locationManager.delegate = self cameraButton.isEnabled = false photoButton.isEnabled = false previewView.session = session switch AVCaptureDevice.authorizationStatus(for: .video) { case .authorized: break case .notDetermined: sessionQueue.suspend() AVCaptureDevice.requestAccess(for: .video, completionHandler: { granted in if !granted { self.setupResult = .notAuthorized } self.sessionQueue.resume() }) default: setupResult = .notAuthorized } */ sessionQueue.async { self.configureSession() } } //字数制限により省略 @IBOutlet private weak var photoButton: UIButton! @IBAction private func capturePhoto(_ photoButton: UIButton) { /* Retrieve the video preview layer's video orientation on the main queue before entering the session queue. We do this to ensure UI elements are accessed on the main thread and session configuration is done on the session queue. */ let videoPreviewLayerOrientation = previewView.videoPreviewLayer.connection?.videoOrientation sessionQueue.async { // Update the photo output's connection to match the video orientation of the video preview layer. if let photoOutputConnection = self.photoOutput.connection(with: .video) { photoOutputConnection.videoOrientation = videoPreviewLayerOrientation! } func configure(location: CLLocation) { var photoSettings = AVCapturePhotoSettings() // Capture HEIF photo when supported, with flash set to auto and high resolution photo enabled. if self.photoOutput.availablePhotoCodecTypes.contains(.hevc) { photoSettings = AVCapturePhotoSettings(format: [AVVideoCodecKey: AVVideoCodecType.hevc]) } if self.videoDeviceInput.device.isFlashAvailable { photoSettings.flashMode = .auto } photoSettings.isHighResolutionPhotoEnabled = true if !photoSettings.__availablePreviewPhotoPixelFormatTypes.isEmpty { photoSettings.previewPhotoFormat = [kCVPixelBufferPixelFormatTypeKey as String: photoSettings.__availablePreviewPhotoPixelFormatTypes.first!] } if self.livePhotoMode == .on && self.photoOutput.isLivePhotoCaptureSupported { // Live Photo capture is not supported in movie mode. let livePhotoMovieFileName = NSUUID().uuidString let livePhotoMovieFilePath = (NSTemporaryDirectory() as NSString).appendingPathComponent((livePhotoMovieFileName as NSString).appendingPathExtension("mov")!) photoSettings.livePhotoMovieFileURL = URL(fileURLWithPath: livePhotoMovieFilePath) } if self.depthDataDeliveryMode == .on && self.photoOutput.isDepthDataDeliverySupported { photoSettings.isDepthDataDeliveryEnabled = true } else { photoSettings.isDepthDataDeliveryEnabled = false } // Use a separate object for the photo capture delegate to isolate each capture life cycle. let photoCaptureProcessor = PhotoCaptureProcessor(with: photoSettings, willCapturePhotoAnimation: { DispatchQueue.main.async { self.previewView.videoPreviewLayer.opacity = 0 UIView.animate(withDuration: 0.25) { self.previewView.videoPreviewLayer.opacity = 1 } } }, livePhotoCaptureHandler: { capturing in /* Because Live Photo captures can overlap, we need to keep track of the number of in progress Live Photo captures to ensure that the Live Photo label stays visible during these captures. */ self.sessionQueue.async { if capturing { self.inProgressLivePhotoCapturesCount += 1 } else { self.inProgressLivePhotoCapturesCount -= 1 } let inProgressLivePhotoCapturesCount = self.inProgressLivePhotoCapturesCount DispatchQueue.main.async { if inProgressLivePhotoCapturesCount > 0 { self.capturingLivePhotoLabel.isHidden = false } else if inProgressLivePhotoCapturesCount == 0 { self.capturingLivePhotoLabel.isHidden = true } else { print("Error: In progress live photo capture count is less than 0") } } } }, completionHandler: { photoCaptureProcessor in // When the capture is complete, remove a reference to the photo capture delegate so it can be deallocated. self.sessionQueue.async { self.inProgressPhotoCaptureDelegates[photoCaptureProcessor.requestedPhotoSettings.uniqueID] = nil } } ) /* The Photo Output keeps a weak reference to the photo capture delegate so we store it in an array to maintain a strong reference to this object until the capture is completed. */ self.inProgressPhotoCaptureDelegates[photoCaptureProcessor.requestedPhotoSettings.uniqueID] = photoCaptureProcessor self.photoOutput.capturePhoto(with: photoSettings, delegate: photoCaptureProcessor) } } } private func setMeta(location: CLLocation) -> Dictionary<String, Any> { // メタデータ //exif情報を準備する let gps = NSMutableDictionary() var exif = Dictionary<String, Any>() var meta = Dictionary<String, Any>() // 位置情報 gps.setObject(location.timestamp, forKey: kCGImagePropertyGPSDateStamp as! NSCopying) gps.setObject(location.timestamp, forKey: kCGImagePropertyGPSTimeStamp as! NSCopying) // 経度の設定 // 緯度情報を取得 var latitude = location.coordinate.latitude var gpsLatitudeRef = "N" // 北緯 if latitude < 0 { latitude = -latitude; // マイナス値はプラス値に反転 gpsLatitudeRef = "S" // 南緯 } gps.setObject(gpsLatitudeRef, forKey: kCGImagePropertyGPSLatitudeRef as! NSCopying) gps.setObject(latitude, forKey: kCGImagePropertyGPSLatitude as! NSCopying) // 経度情報を取得 var longitude = location.coordinate.longitude var gpsLongitudeRef = "E" // 東経 if longitude < 0 { longitude = -longitude; // マイナス値はプラス値に反転 gpsLongitudeRef = "W" // 西経 } gps.setObject(gpsLongitudeRef, forKey: kCGImagePropertyGPSLongitudeRef as! NSCopying) gps.setObject(longitude, forKey: kCGImagePropertyGPSLongitude as! NSCopying) gps.setObject(0, forKey: kCGImagePropertyGPSAltitudeRef as! NSCopying) gps.setObject(0, forKey: kCGImagePropertyGPSAltitude as! NSCopying) exif[kCGImagePropertyGPSDictionary as String] = gps exif[kCGImagePropertyExifUserComment as String] = "コメント" meta[kCGImagePropertyExifDictionary as String] = exif return meta } //字数制限により省略 extension AVCaptureVideoOrientation { init?(deviceOrientation: UIDeviceOrientation) { switch deviceOrientation { case .portrait: self = .portrait case .portraitUpsideDown: self = .portraitUpsideDown case .landscapeLeft: self = .landscapeRight case .landscapeRight: self = .landscapeLeft default: return nil } } init?(interfaceOrientation: UIInterfaceOrientation) { switch interfaceOrientation { case .portrait: self = .portrait case .portraitUpsideDown: self = .portraitUpsideDown case .landscapeLeft: self = .landscapeLeft case .landscapeRight: self = .landscapeRight default: return nil } } } extension AVCaptureDevice.DiscoverySession { var uniqueDevicePositionsCount: Int { var uniqueDevicePositions: [AVCaptureDevice.Position] = [] for device in devices { if !uniqueDevicePositions.contains(device.position) { uniqueDevicePositions.append(device.position) } } return uniqueDevicePositions.count } }

###試したこと
func configure(location: CLLocation)の部分を消せば撮影できるのですが、今度は位置情報が記録されなくなりました。位置情報のプライバシーは設定済みです。

###補足情報(言語/FW/ツール等のバージョンなど)

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

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

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

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

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

guest

回答1

0

さいしょからかきなおしてみては

投稿2019/03/26 14:55

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問