xcode 使用自定义相机 iOS 11.0 Swift 4 拍照。更新错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46478262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Taking photo with custom camera iOS 11.0 Swift 4. Update error
提问by 0ndre_
I have a custom camera in my app and it worked fine, but after the new update I am getting this error:
我的应用程序中有一个自定义相机,它运行良好,但在新更新后,我收到此错误:
'jpegPhotoDataRepresentation(forJPEGSampleBuffer:previewPhotoSampleBuffer:)' was deprecated in iOS 11.0: Use -[AVCapturePhoto fileDataRepresentation] instead.
'jpegPhotoDataRepresentation(forJPEGSampleBuffer:previewPhotoSampleBuffer:)' 在 iOS 11.0 中被弃用:改用 -[AVCapturePhoto fileDataRepresentation]。
This is the line where I'm getting that error:
这是我收到该错误的行:
guard let imageData =
AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else {
return
}
This is my whole function (if needed):
这是我的全部功能(如果需要):
//Take pic function
func photoOutput(_ captureOutput: AVCapturePhotoOutput,
didFinishProcessingPhoto photoSampleBuffer: CMSampleBuffer?,
previewPhoto previewPhotoSampleBuffer: CMSampleBuffer?,
resolvedSettings: AVCaptureResolvedPhotoSettings,
bracketSettings: AVCaptureBracketedStillImageSettings?,
error: Error?) {
// Make sure we get some photo sample buffer
guard error == nil,
let photoSampleBuffer = photoSampleBuffer else {
print("Error capturing photo: \(String(describing: error))")
return
}
// Convert photo same buffer to a jpeg image data by using // AVCapturePhotoOutput
guard let imageData =
AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else {
return
}
let dataProvider = CGDataProvider(data: imageData as CFData)
let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.absoluteColorimetric)
let image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.right)
self.tempImageView.image = image
}
My guestion is: What should I use instead to make it work?
我的客人是:我应该用什么来使它工作?
Thank you.
谢谢你。
回答by Vini App
In iOS 11, you should use like this :
在 iOS 11 中,您应该像这样使用:
@available(iOS 11.0, *)
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
let imageData = photo.fileDataRepresentation()
}
回答by infinity_coding7
Thanks @Vini App, I tried the code it worked for me, I posted my code for image capturing and processing, hope that will help people who needs similar function in their app.
谢谢@Vini App,我尝试了它对我有用的代码,我发布了我的图像捕获和处理代码,希望能帮助在他们的应用程序中需要类似功能的人。
First you need to setup your video capturing device, search it google here is an example https://gist.github.com/tad-iizuka/fc35bc7835920c0b8b84e316f83e3a40
首先你需要设置你的视频捕获设备,在谷歌搜索它是一个例子https://gist.github.com/tad-iizuka/fc35bc7835920c0b8b84e316f83e3a40
Makes sure that you need to define photoSetting
at the top
确保您需要photoSetting
在顶部定义
...
var photoSetting = AVCapturePhotoSettings()
...
Configure the photo setting either in viewDidLoad()
or viewWillAppear()
在viewDidLoad()
或 中配置照片设置viewWillAppear()
// Configure camera
photoSetting = AVCapturePhotoSettings.init(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])
photoSetting.isAutoStillImageStabilizationEnabled = true
photoSetting.flashMode = .off
Then use the following function to process buffered image data
然后使用下面的函数来处理缓冲的图像数据
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
// Check if there is any error in capturing
guard error == nil else {
print("Fail to capture photo: \(String(describing: error))")
return
}
// Check if the pixel buffer could be converted to image data
guard let imageData = photo.fileDataRepresentation() else {
print("Fail to convert pixel buffer")
return
}
// Check if UIImage could be initialized with image data
guard let capturedImage = UIImage.init(data: imageData , scale: 1.0) else {
print("Fail to convert image data to UIImage")
return
}
// Get original image width/height
let imgWidth = capturedImage.size.width
let imgHeight = capturedImage.size.height
// Get origin of cropped image
let imgOrigin = CGPoint(x: (imgWidth - imgHeight)/2, y: (imgHeight - imgHeight)/2)
// Get size of cropped iamge
let imgSize = CGSize(width: imgHeight, height: imgHeight)
// Check if image could be cropped successfully
guard let imageRef = capturedImage.cgImage?.cropping(to: CGRect(origin: imgOrigin, size: imgSize)) else {
print("Fail to crop image")
return
}
// Convert cropped image ref to UIImage
imageToSave = UIImage(cgImage: imageRef, scale: 1.0, orientation: .down)
UIImageWriteToSavedPhotosAlbum(imageToSave, nil, nil, nil)
// Stop video capturing session (Freeze preview)
captureSession.stopRunning()
}
In this function, pixel buffer is converted to image data in the format specified by photoSetting
and then cropped to the size you want.
在这个函数中,像素缓冲区被转换为指定格式的图像数据photoSetting
,然后裁剪到你想要的大小。
You can create a button in IB to call the capturing function above
可以在IB中创建一个按钮来调用上面的抓包函数
@IBAction func onTakePhoto(_ sender: UIButton) {
if let videoConnection = videoOutput.connection(with: AVMediaType.video) {
// Adjust the orientaion of captured image
let capturePhotoSetting = AVCapturePhotoSettings.init(from: photoSetting)
videoConnection.videoOrientation = (previewLayer.connection?.videoOrientation)!
// Save captured photo to system album
self.videoOutput.capturePhoto(with: capturePhotoSetting, delegate: self)
}
}
回答by karan
For iOS 11.0 and lower both I handled
对于 iOS 11.0 及更低版本,我都处理了
var photoOutput : AVCapturePhotoOutput?
if #available(iOS 11.0, *)
{
photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format:[AVVideoCodecKey:AVVideoCodecType.jpeg])], completionHandler: nil)
}
else
{
photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format:[AVVideoCodecKey:AVVideoCodecJPEG])], completionHandler: nil)
}
回答by nitin.agam
Use this code (Swift 5), it is working fine for me.
使用此代码(Swift 5),它对我来说很好用。
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
guard let imageData = photo.fileDataRepresentation() else { return }
let previewImage = UIImage(data: imageData)
}