ios - 如何使用 AVFoundation 将图像捕获到文档目录

标签 ios swift avfoundation

friend 我使用 AVFoundation 和 Swift 来拍摄照片。下面是我的代码

func captureImage(completion: @escaping (UIImage?, Error?) -> Void) {
    guard let captureSession = captureSession, captureSession.isRunning     
else { completion(nil, CameraControllerError.captureSessionIsMissing); return }

    let settings = AVCapturePhotoSettings()
    settings.flashMode = self.flashMode

    self.photoOutput?.capturePhoto(with: settings, delegate: self)
    self.photoCaptureCompletionBlock = completion
}

如何使用 AVFoundation 仅将图像捕获到文档应用程序目录? 我想也许可以捕获到标准相机胶卷,然后将图像保存到文档并从相机胶卷中删除。但我认为这是个坏主意。

如何仅保存到文档? 请帮忙

最佳答案

我还没有使用新的图像捕获流程,但我在文档中做了一些研究。

引用 AVCapturePhoto 中的文档:

When you capture photos with the AVCapturePhotoOutput class, your delegate object receives each resulting image and related data in the form of an AVCapturePhoto object. This object is an immutable wrapper from which you can retrieve various results of the photo capture.

如果你看AVCapturePhoto在Xcode文档中,有一个方法fileDataRepresentation()它将返回准备写入文件的图像数据(数据格式由您创建的 AVCapturePhotoSettings 中的设置确定)。您只需将该数据保存到您的文档目录即可。

AVCapturePhoto还有一个功能cgImageRepresentation()这将为您提供捕获照片的 CGImage,然后您可以根据需要对其进行处理。

关于ios - 如何使用 AVFoundation 将图像捕获到文档目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52237335/

相关文章:

ios - 如何在 calabash ios 中通过标题标签查找 UIButton?

ios - 使用 Alamofire.upload 时出现段错误 : 11,

ios - 在 Swift 中使用自定义函数调用的 snackbar

ios - 从不同的 View 模型调用服务时绑定(bind)事件未触发

ios - 启动参数(NSDoubleLocalizedStrings、NSShowNonLocalizedStrings)不起作用

ios - 如何在 AVFoundation 中混合/屏蔽视频?

swift - AVAudioPlayerNode.Stop 上的 AVFoundation Mutex 互锁

ios - avplayer无法播放通过IOS Objective-C中的设备相机捕获的视频中的音频

ios - 没有为 URL 方案 itms-apps 注册处理程序 - 评分按钮模拟器

访问 NSUserDefaults 时,Release Build 会发生 Swift 崩溃