使用自定义相机拍照 iOS 11.0 Swift 4.更新错误
Taking photo with custom camera iOS 11.0 Swift 4. Update error
我的应用程序中有一个自定义相机,它工作正常,但在新更新后我收到此错误:
'jpegPhotoDataRepresentation(forJPEGSampleBuffer:previewPhotoSampleBuffer:)' was deprecated in iOS 11.0: Use -[AVCapturePhoto fileDataRepresentation] instead.
这是我遇到错误的行:
guard let imageData =
AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else {
return
}
这是我的全部功能(如果需要的话):
//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
}
我的建议是:我应该用什么来让它工作?
谢谢。
在iOS11中,你应该这样使用:
@available(iOS 11.0, *)
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
let imageData = photo.fileDataRepresentation()
}
感谢@Vini App,我尝试了适合我的代码,我发布了我的图像捕获和处理代码,希望能帮助那些在他们的应用程序中需要类似功能的人。
首先你需要设置你的视频捕获设备,搜索它google这里有一个例子https://gist.github.com/tad-iizuka/fc35bc7835920c0b8b84e316f83e3a40
确保您需要在顶部定义 photoSetting
...
var photoSetting = AVCapturePhotoSettings()
...
在 viewDidLoad()
或 viewWillAppear()
中配置照片设置
// Configure camera
photoSetting = AVCapturePhotoSettings.init(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])
photoSetting.isAutoStillImageStabilizationEnabled = true
photoSetting.flashMode = .off
然后使用以下函数处理缓冲图像数据
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()
}
在这个函数中,像素缓冲区被转换成photoSetting
指定格式的图像数据,然后裁剪成你想要的大小。
您可以在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)
}
}
对于 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)
}
使用此代码 (Swift 5),对我来说效果很好。
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
guard let imageData = photo.fileDataRepresentation() else { return }
let previewImage = UIImage(data: imageData)
}
我的应用程序中有一个自定义相机,它工作正常,但在新更新后我收到此错误:
'jpegPhotoDataRepresentation(forJPEGSampleBuffer:previewPhotoSampleBuffer:)' was deprecated in iOS 11.0: Use -[AVCapturePhoto fileDataRepresentation] instead.
这是我遇到错误的行:
guard let imageData =
AVCapturePhotoOutput.jpegPhotoDataRepresentation(forJPEGSampleBuffer: photoSampleBuffer, previewPhotoSampleBuffer: previewPhotoSampleBuffer) else {
return
}
这是我的全部功能(如果需要的话):
//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
}
我的建议是:我应该用什么来让它工作?
谢谢。
在iOS11中,你应该这样使用:
@available(iOS 11.0, *)
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
let imageData = photo.fileDataRepresentation()
}
感谢@Vini App,我尝试了适合我的代码,我发布了我的图像捕获和处理代码,希望能帮助那些在他们的应用程序中需要类似功能的人。
首先你需要设置你的视频捕获设备,搜索它google这里有一个例子https://gist.github.com/tad-iizuka/fc35bc7835920c0b8b84e316f83e3a40
确保您需要在顶部定义 photoSetting
...
var photoSetting = AVCapturePhotoSettings()
...
在 viewDidLoad()
或 viewWillAppear()
// Configure camera
photoSetting = AVCapturePhotoSettings.init(format: [AVVideoCodecKey: AVVideoCodecType.jpeg])
photoSetting.isAutoStillImageStabilizationEnabled = true
photoSetting.flashMode = .off
然后使用以下函数处理缓冲图像数据
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()
}
在这个函数中,像素缓冲区被转换成photoSetting
指定格式的图像数据,然后裁剪成你想要的大小。
您可以在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)
}
}
对于 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)
}
使用此代码 (Swift 5),对我来说效果很好。
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
guard let imageData = photo.fileDataRepresentation() else { return }
let previewImage = UIImage(data: imageData)
}