栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

无法使用AVCapturePhotoOutput捕获照片swift + Xcode

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

无法使用AVCapturePhotoOutput捕获照片swift + Xcode

你快到了。

输出为
AVCapturePhotoOutput

查看

AVCapturePhotoOutput

文档以获取更多帮助。

这些是拍摄照片的步骤。

  1. 创建一个
    AVCapturePhotoOutput
    对象。使用其属性来确定支持的捕获设置并启用某些功能(例如,是否捕获实时照片)。
  2. 创建并配置
    AVCapturePhotoSettings
    对象以选择特定捕获的功能和设置(例如,启用图像稳定还是闪光灯)。
  3. 通过将照片设置对象
    capturePhoto(with:delegate:)
    与实现
    AVCapturePhotoCaptureDelegate
    协议的委托对象一起传递给方法来捕获图像 。然后,照片捕获输出将呼叫您的代表,以在捕获过程中将重大事件通知您。

在您的

clickCapture
方法上具有以下代码,并且不要忘记在类中进行确认和实现委托。

let settings = AVCapturePhotoSettings()let previewPixelType = settings.availablePreviewPhotoPixelFormatTypes.first!let previewFormat = [kCVPixelBufferPixelFormatTypeKey as String: previewPixelType,       kCVPixelBufferWidthKey as String: 160,       kCVPixelBufferHeightKey as String: 160,       ]settings.previewPhotoFormat = previewFormatself.cameraOutput.capturePhoto(with: settings, delegate: self)

输出为
AVCaptureStillImageOutput

如果您打算通过视频连接拍摄照片。您可以按照以下步骤操作。

步骤1:建立连线

if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) {  // ...  // Code for photo capture goes here...}

步骤2:拍摄相片

  • captureStillImageAsynchronouslyFromConnection
    上调用该函数
    stillImageOutput
  • sampleBuffer
    表示被捕捉的数据。

stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { (sampleBuffer, error) -> Void in  // ...  // Process the image data (sampleBuffer) here to get an image file we can put in our captureImageView})

步骤3:处理图像数据

  • 我们将需要采取一些步骤来处理在sampleBuffer中找到的图像数据,以便最终获得一个UIImage,我们可以将其插入到captureImageView中并在应用程序的其他位置轻松使用。

if sampleBuffer != nil {  let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)  let dataProvider = CGDataProviderCreateWithCFData(imageData)  let cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, CGColorRenderingIntent.RenderingIntentDefault)  let image = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)  // ...  // Add the image to captureImageView here...}

步骤4:储存图片

根据您的需要将图像保存到照片库或在图像视图中显示


有关更多详细信息,请查看 “ 捕捉照片” 下的“
创建自定义相机视图指南 ”。 __



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/636365.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号