主视图崩溃时的子视图将图像从AVFoundation分配给UIImageView时

Far*_*had 8 avfoundation nsthread ios swift

我已经创建了一个自定义AVFoundation摄像头并作为子视图加载到另一个视图中使用@IBDesignable,在拍摄图像后我想将其分配给UIImageView,但是当按下按钮时,自定义摄像头视图(UIView)会崩溃并向上移动屏幕,而不是将其分配给UIImageView,这发生在自定义摄像头视图加载开始时,然后当我再次加载自定义摄像头视图时,它会将图像分配给UIImageView.

捕获图像方法:

@IBAction func captureImage(sender: AnyObject) {
        if let stillOutput = self.captureImageOutput {
            // we do this on another thread so that we don't hang the UI
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
                //find the video connection
                var videoConnection : AVCaptureConnection?
                for connecton in stillOutput.connections {
                    //find a matching input port
                    for port in connecton.inputPorts!{
                        if port.mediaType == AVMediaTypeVideo {
                            videoConnection = connecton as? AVCaptureConnection
                            break //for port
                        }
                    }

                    if videoConnection  != nil {
                        break// for connections
                    }
                }
                if videoConnection  != nil {
                    stillOutput.captureStillImageAsynchronouslyFromConnection(videoConnection){
                        (imageSampleBuffer : CMSampleBuffer!, _) in

                        let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageSampleBuffer)
                        let pickedImage: UIImage = UIImage(data: imageData, scale: 1.0)!

                        dispatch_async(dispatch_get_main_queue(), {
                            self.capturePhoto.image = pickedImage
                        })
                    }
                }
            }
        }

    }
Run Code Online (Sandbox Code Playgroud)

我也尝试了UIImageView中的dispatch_sync无负载,但屏幕不会崩溃.

dispatch_sync(dispatch_get_main_queue(), {
      self.capturePhoto.image = pickedImage
}) 
Run Code Online (Sandbox Code Playgroud)

pea*_*212 -1

我刚刚测试了它,这对我有用(Swift 2.0)

override func viewDidLoad() {
    super.viewDidLoad()
    imageView = UIImageView(frame: self.view.frame)
    self.view.addSubview(imageView)
}

func takeImage() {

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {

        guard let videoConnection = self.stillImageOutput.connectionWithMediaType(AVMediaTypeVideo) else { return }
        guard videoConnection.enabled else { return }

        self.stillImageOutput.captureStillImageAsynchronouslyFromConnection(videoConnection) { (buffer: CMSampleBuffer!, error: NSError!) -> Void in

            let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)
            let image: UIImage = UIImage(data: imageData, scale: 1.0)!

            dispatch_async(dispatch_get_main_queue(), {
                self.imageView.image = image
                print("picture taken")
            })
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

图像已成功拍摄并添加到 ViewController。如果这对您不起作用,也许您可​​以详细解释您的设置,以便我可以尝试重现错误/崩溃。