捕获图像方法:
@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.如果这对您不起作用,也许您可以在设置上解释更多,以便我可以尝试重现错误/崩溃.
总结以上是内存溢出为你收集整理的ios – 主视图崩溃时的子视图将AVFoundation中的图像分配给UIImageView时全部内容,希望文章能够帮你解决ios – 主视图崩溃时的子视图将AVFoundation中的图像分配给UIImageView时所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)