You'll learn how to access the camera on your device to capture still images.
This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4033-beginning-video-with-avfoundation/lessons/3
You'll learn how to access the camera on your device to capture still images.
Really enjoy the AVFoundation video tutorials!
However im struggling to fix a couple errors from the swift 3 update.
// .default was deprecated in iOS 8
func videoQueue() â DispatchQueue {
return DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.default)
}
Online ive read it should now be the code below but not sure how to implement it into the project
DispatchQueue.global(qos: .background).async {
}
The other two errors are:
Cannot convert value of type â(Bool, NSError?) â Voidâ to expected argument type â((Bool, Error?) â Void)?â
// MARK: - Helpers
func savePhotoToLibrary(_ image: UIImage) {
let photoLibrary = PHPhotoLibrary.shared()
photoLibrary.performChanges({
PHAssetChangeRequest.creationRequestForAsset(from: image)
}) { (success: Bool, error: NSError?) â Void in
if success {
// Set thumbnail
self.setPhotoThumbnail(image)
} else {
print(âError writing to photo library: (error!.localizedDescription)â)
}
}
}
Cannot convert value of type â(CMSampleBuffer!, NSError!) â Voidâ to expected argument type â((CMSampleBuffer?, Error?) â Void)!â
imageOutput.captureStillImageAsynchronously(from: connection) {
(sampleBuffer: CMSampleBuffer!, error: NSError!) â Void in
if sampleBuffer != nil {
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
let image = UIImage(data: imageData)
let photoBomb = self.penguinPhotoBomb(image!)
self.savePhotoToLibrary(photoBomb)
} else {
print(âError capturing photo: (error.localizedDescription)â)
}
}
}
elliott_19, Iâm not sure if you already figured this out. I ran into some of the same issues as you. I discovered the following fixes to ensure compatibility with Swift 3.
For the dispatch errors in startSession() and stopSession() I changed them to as follows:
func startSession() {
if !captureSession.isRunning {
DispatchQueue.main.async {
self.captureSession.startRunning()
}
}
}
func stopSession() {
if captureSession.isRunning {
DispatchQueue.main.async {
self.captureSession.stopRunning()
}
}
}
For 1) with errors about 'Cannot convert value of type â(Bool, NSError!) â Voidâ, Swift 3 now uses just Error, so simply changing this line to the below resolved the error for me:
}) { (success: Bool, error: Error?) -> Void in
I havenât completed the tutorial videos yet, but I expect your error 2) would also be resolved by changing from NSError to Error.
elliott_19, hereâs what I had to do to fix the errors Xcode spat out for the capturePhoto function:
// MARK: - Capture photo
@IBAction func capturePhoto(sender: AnyObject) {
let connection = imageOutput.connection(withMediaType: AVMediaTypeVideo)
if (connection?.isVideoOrientationSupported)! {
connection?.videoOrientation = currentVideoOrientation()
}
imageOutput.captureStillImageAsynchronously (from: connection) {
(sampleBuffer: CMSampleBuffer?, error: Error?) -> Void in
if sampleBuffer != nil {
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
let image = UIImage(data: imageData!)
let photoBomb = self.penguinPhotoBomb(image!)
self.savePhotoToLibrary(photoBomb)
} else {
print("Error capturing photo: \(error?.localizedDescription)")
}
}
}
hey , I have some problems with the code . Xcode says that there is no âcaptureStillImageAsynchronouslyâ for "AVCapturePhotoOutput. I used âAVCapturePhotoOutputâ because Xcode says that there is no more âAVCaptureStillImageOutput()â.
Hey skyrocketsw, please update your tutorial because it is outdated. I tried the free video tutorial on how to capture images and it has some error in 2 different functions:
I would love to pay for the tutorial series if was actually up to date!
Thanks!
As of this commentâs date, Xcode wonât open the project materials.
It says that Xcode 10.1 is required to open or migrate Swift 3.x projects.