前提・実現したいこと
Macで写真撮影→特定場所に保存というアプリケーションを作成しております。
(この質問とこの質問)
そこで写真撮影部分のロジックの実装はできたのですが、Data型に格納したデータを保存する方法がわかりません。
(正確にはプログラムは組めているのですがtry - catch
でcatchに入ってしまって保存されません。)
該当のソースコード
Swift
1let myAVConnection = myImageOutput.connection(with: AVMediaType.video) 2 myImageOutput.captureStillImageAsynchronously(from: myAVConnection!, completionHandler: { 3 (imageDataBuffer,error) -> Void in 4 let myImageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataBuffer!) 5 do{ 6 try myImageData!.write(to: URL(string: "/Users/私のユーザー名" + "/Pictures/test.jpeg")!) 7 }catch{ 8 print("catch通過");//catchを通ったことがわかるように 9 } 10}) 11 12//もちろんPicturesフォルダに画像はなく、ログにはcatch通過と表示
試したこと
######AppSandboxでフォルダーに対する権限を設置しました → 書き込みされず
Type | Permission & Access |
---|---|
User Selected File | Read/Write |
Downloads Folder | Read/Write |
Pictures Folder | Read/Write |
Music Folder | Read/Write |
Movies Folder | Read/Write |
######tryを外してみましたが → コンパイル時エラー | |
```Swift | |
let myImageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataBuffer!) |
/*do{ try*/ myImageData!.write(to: URL(string: "/Users/私のユーザー名" + "/Pictures/test.jpeg")!) /*}catch{ print("catch通過"); }*/
})
![Call can throw. but it is not marked with 'try and the error'](43af9132bd0388d18c2b173e20d92dd4.png) ### 補足情報(FW/ツールのバージョンなど) macos Mojave version 10.14.1 Xcode Version 10.1(10B61) ※ファイルのパスを直書きしていますが、個人で使用するソフトウェアなので問題ありません。 ###追記(MasakiHoriさんより) > catch内をprint(error)に変えて出力されたエラーを質問に追加してください ```Swift let myImageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataBuffer!) do{ try myImageData!.write(to: URL(string: "/Users/私のユーザー名" + "/Pictures/test.jpeg")!) }catch{ print(error); } })
としたところ、
Log
1[時刻][プロジェクト名][PIDと思われる番号] CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme 2Error Domain=NSCocoaErrorDomain Code=518 "The file couldn’t be saved because the specified URL type isn’t supported." UserInfo={NSURL=/Users/[ユーザー名]/Pictures/test.jpeg}
と出てきました。(機密情報は[]で隠させていただき概要だけ書いておきます。)
回答1件
あなたの回答
tips
プレビュー