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

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

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

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

Q&A

解決済

1回答

2929閲覧

swiftでAVCapturePhotoSettingsのプロパティを使って写真に位置情報を記録したい。

huyuhimituhiro

総合スコア7

Swift

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

0グッド

1クリップ

投稿2018/01/14 04:06

編集2018/01/18 00:44

###前提・実現したいこと
swiftでAVCapturePhotoSettingsのプロパティを使って写真に位置情報を記録したい。
アップルのサンプルコードと、こちらのサイトを参考にさせていただきました。https://qiita.com/BlueEventHorizon/items/cda4661d98af768ae5d9

###発生している問題・エラーメッセージ
カメラで撮影ができない。位置情報を保存するプロパティにエラーが出来る。

Expected '{' to start getter definition

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

import UIKit import CoreLocation import AVFoundation import ImageIO import Photos class CameraViewController: UIViewController, CLLocationManagerDelegate, AVCaptureFileOutputRecordingDelegate { override func viewDidLoad() { super.viewDidLoad() 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() } } //字数制限により省略 var metadata: [String : Any] { get set } 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 } }

###試したこと
var metadata: [String : Any] { get set }に格納するのは分かっていますが、その書き方が分かりません。

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

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

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

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

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

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

fuzzball

2018/01/16 04:39

どこでエラーが出るのでしょうか?
huyuhimituhiro

2018/01/16 07:25

申し訳ございません。 var metadata: [String : Any] { get set }の部分です。
guest

回答1

0

ベストアンサー

swift

1var metadata: [String : Any] { get set }

というのは、そういう風に定義されてるいるよ、ということで自分で書かなくてもいいです。
参考にしている記事のように、

swift

1photoSettings.metadata = self.setMeta(location: location)

でいけると思うのですが、同じようにしない(出来ない)理由がありますか?

投稿2018/01/16 07:42

fuzzball

総合スコア16731

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

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

huyuhimituhiro

2018/01/16 08:07

そういう風定義されているとの説明だけで、書く必要なかったのですね。ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問