How to convert code AVFoundation objective c to Swift?

て烟熏妆下的殇ゞ 提交于 2019-12-04 06:32:58
for (AVCaptureInputPort *port in [connection inputPorts]) { )

Arrays of AnyObject should be cast to arrays of your actual type before interating, like this:

for (port in connection.inputPorts as AVCaptureInputPort[]) { }

In terms of blocks to closures, you just have to get the syntax correct.

stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) {
    (imageSampleBuffer, error) in // This line defines names the inputs
    //...
}

Note that this also uses Trailing Closure Syntax. Do read up on the docs more!

EDIT: In terms of initializers, they now look like this:

let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageSampleBuffer)
self.processImage(UIImage(data:imageData))

Try this

    var videoConnection :AVCaptureConnection?
   if let videoConnection = self.stillImageOutput.connectionWithMediaType(AVMediaTypeVideo){
     self.stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: { (buffer:CMSampleBuffer!, error: NSError!) -> Void in
     if let exifAttachments = CMGetAttachment(buffer, kCGImagePropertyExifDictionary, nil) {
         let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)
         self.previewImage.image = UIImage(data: imageData)
         UIImageWriteToSavedPhotosAlbum(self.previewImage.image, nil, nil, nil)
      }
     })
   }

This should answer the problem with the ports:

if let videoConnection = stillImageOuput.connectionWithMediaType(AVMediaTypeVideo){//take a photo here}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!