我在使用Swift 3和Xcode 8.3(8E162)将数据写入iOS上的磁盘时遇到问题。所有运行iOS10.3的iPhone5,iPhone6 plus和iPhone7都经过测试,但无济于事。
在我的视图课程中,我声明:
var takePhoto: AVCapturePhotoOutput!
和一个叫做的IBAction:
takePhoto.capturePhoto(with: settings, delegate: self)
当触发IBAction(按钮点击)时,将调用capturePhoto()
及其委托。我试图写出不起作用的文件。将图像添加到当前视图确实有效,因此我知道捕获的图像缓冲区很好。
func capture(_ captureOutput: AVCapturePhotoOutput, didFinishProcessingPhotoSampleBuffer photoSampleBuffer: CMSampleBuffer?, previewPhotoSampleBuffer: CMSampleBuffer?, resolvedSettings: AVCaptureResolvedPhotoSettings, bracketSettings: AVCaptureBracketedStillImageSettings?, error: Error?) {
// Create file path using FileManager
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
let filename = documentsURL?.appendingPathComponent("photo.jpg")
// Try write out the file
do{
let writeToFile = try dataImage.write(to: filename!, options: .atomic)
print("WRITING out file", writeToFile)
}
catch {
print("ERROR writing out file")
}
// Check if the file was created
if FileManager.default.fileExists(atPath: (filename?.absoluteString)!) {
print("YAY! Image " + (filename?.absoluteString)! + " exists")
}
else {
print("BOO! The file doesn't exist: " + (filename?.absoluteString)!)
}
// Add the image to the current view
let viewImage = UIImageView(frame: CGRect(x: 50, y: 50, width: 100, height: 100))
viewImage.image = UIImage(data: dataImage)
viewImage.contentMode = .scaleAspectFit
self.view.addSubview(viewImage)
}
write()
没有throw
,fileExists
失败
WRITING out file ()
BOO! The file doesn't exist:
file:///var/mobile/Containers/Data/Application/1E77951B-BEA5-48AD-ADD3-5DCB289A94C0/Documents/photo.jpg
我错过了什么?