ios - 自定义相机需要很长时间才能保存图像(Swift 和 AVFoundation)

标签 ios swift uiimage avfoundation

我下面的代码是一个可以工作的自定义相机。我的目标是在这里拍照并将其大小调整为正方形。之后,我将图像分配给 UIImage,然后通过 segue 传递到后 Controller 。转场最终发生在图像本身保存之前。我知道这一点是因为会发生转场,几秒钟后控制台将打印 UIImage 的值。

class CameraViewController: UIViewController {


@IBOutlet weak var photoButton: UIButton!
@IBOutlet weak var previewView: UIView!


var captureSession: AVCaptureSession?
var stillImageOutput: AVCaptureStillImageOutput?
var previewLayer: AVCaptureVideoPreviewLayer?
var buttonImage = UIImage(named: "CameraButton.png")
var captImage = UIImage()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    photoButton.setBackgroundImage(buttonImage, forState: .Normal)

    captureSession = AVCaptureSession()
    captureSession!.sessionPreset = AVCaptureSessionPresetPhoto

    let backCamera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)

    var error: NSError?
    var input: AVCaptureDeviceInput!
    do {
        input = try AVCaptureDeviceInput(device: backCamera)
    } catch let error1 as NSError {
        error = error1
        input = nil
    }

    if error == nil && captureSession!.canAddInput(input) {
        captureSession!.addInput(input)

        stillImageOutput = AVCaptureStillImageOutput()
        stillImageOutput!.outputSettings = [AVVideoCodecKey: AVVideoCodecJPEG]
        if captureSession!.canAddOutput(stillImageOutput) {
            captureSession!.addOutput(stillImageOutput)

            previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
            previewLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill
            previewLayer!.connection?.videoOrientation = AVCaptureVideoOrientation.Portrait
            previewView.layer.addSublayer(previewLayer!)

            captureSession!.startRunning()
        }
    }

}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    previewLayer!.frame = previewView.bounds
}

@IBAction func didPressTakePhoto(sender: UIButton) {

    if let videoConnection = stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo) {
        videoConnection.videoOrientation = AVCaptureVideoOrientation.Portrait
        stillImageOutput?.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {(sampleBuffer, error) in
            if (sampleBuffer != nil) {
                let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
                let dataProvider = CGDataProviderCreateWithCFData(imageData)
                let cgImageRef = CGImageCreateWithJPEGDataProvider(dataProvider, nil, true, CGColorRenderingIntent.RenderingIntentDefault)

                var image = UIImage(CGImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.Right)
                //Crop the image to a square
                let imageSize: CGSize = image.size
                let width: CGFloat = imageSize.width
                let height: CGFloat = imageSize.height
                if width != height {
                    let newDimension: CGFloat = min(width, height)
                    let widthOffset: CGFloat = (width - newDimension) / 2
                    let heightOffset: CGFloat = (height - newDimension) / 2
                    UIGraphicsBeginImageContextWithOptions(CGSizeMake(newDimension, newDimension), false, 0.0)
                    image.drawAtPoint(CGPointMake(-widthOffset, -heightOffset), blendMode: .Copy, alpha: 1.0)
                    image = UIGraphicsGetImageFromCurrentImageContext()
                    UIGraphicsEndImageContext()
                    self.captImage = image

                    print(self.captImage)
                }

            }
        })
        self.performSegueWithIdentifier("fromCustomCamera", sender: self)
    }


}

@IBAction func didPressTakeAnother(sender: AnyObject) {
    captureSession!.startRunning()
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "fromCustomCamera"{
        let svc = segue.destinationViewController as! PostViewController
        svc.imageRec = self.captImage
    }
}
}

最佳答案

如果要确保调用完成后执行,则必须在异步函数的完成处理程序中调用 performSegueWithIdentifier

关于ios - 自定义相机需要很长时间才能保存图像(Swift 和 AVFoundation),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35308204/

相关文章:

swift - 拖动和重新排序 - 带部分的 UICollectionview

ios - UIImageNamed -> 按大小类获取 Assets

ios - 在 UiTableView 之上创建一个按钮

ios - NSManagedObject 的保留周期是否仍然可行?

ios - 如何创建一个计算器,通过一个输入填充多个具有不同值的标签字段?

ios - 根据 Xcode UI 测试是否运行定义全局变量

SwiftUI 如何将数据传递到 ObservedObject 函数

ios - 缩小 UIImage 可以吗?

ios - 用另一种颜色替换图像中的特定颜色

ios - 如何在 Xcode 中制作跟随手指位置的左/右转场