Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

Video Tutorial: Beginning Video with AVFoundation Part 2: Capturing Media: Images

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

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:

  1. 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)”)
    }
    }
    }

  2. 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:

  • imageOutput.captureStillImageAsynchronously()
  • savePhotoToLibrary

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. :frowning_face:

It says that Xcode 10.1 is required to open or migrate Swift 3.x projects.

@bendrexl Do you still have issues with this?