ios – 主视图崩溃时的子视图将AVFoundation中的图像分配给UIImageView时

前端之家收集整理的这篇文章主要介绍了ios – 主视图崩溃时的子视图将AVFoundation中的图像分配给UIImageView时前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经创建了一个自定义AVFoundation摄像头并作为子视图加载到另一个视图中使用@IBDesignable,在拍摄图像后我想将其分配给UI ImageView,但是当按下按钮时,自定义摄像头视图(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
                        })
                    }
                }
            }
        }

    }

我也尝试过dispatch_sync并且在UIImageView中没有任何加载,但屏幕不会崩溃.

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

解决方法

我只是测试了它,这对我有用(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")
            })
        }
    }

}

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

原文链接:https://www.f2er.com/iOS/333750.html

猜你在找的iOS相关文章