您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

你快到了。

查看AVCapturePhotoOutput文档获取更多帮助。

这些是拍摄照片的步骤。

在您的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 = previewFormat
self.cameraOutput.capturePhoto(with: settings, delegate: self)

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

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

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
})

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...
}

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

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

Swift 2022/1/1 18:16:06 有258人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶