不可能在2个视图控制器之间传递UIImage(无故事板)
问题描述:
在第一个ViewController(AViewController)中,我设置了相机。当图片被捕获时,我呈现另一个包含UIImageView的ViewController(BViewController)。问题是BViewController中的UIImageView没有显示在AViewController中捕获的图片。我指定我不使用故事板。不可能在2个视图控制器之间传递UIImage(无故事板)
有没有人有想法解决这个失败?我错过了什么 ?感谢您的帮助!
class AViewController: UIViewController {
...
func capture(){
if let videoConnection = stillImageOutput!.connection(withMediaType: AVMediaTypeVideo) {
videoConnection.videoOrientation = AVCaptureVideoOrientation.portrait
stillImageOutput?.captureStillImageAsynchronously(from: videoConnection, completionHandler: {(sampleBuffer, error) in
if (sampleBuffer != nil) {
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer)
let dataProvider = CGDataProvider(data: imageData as! CFData)
let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.defaultIntent)
let imageSaved = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: UIImageOrientation.right)
self.present(BViewController(), animated: false, completion: { Void in
BViewController().integrate(image: imageSaved)
})
}
}
}
}
=========================================== =====================
class BViewController : UIViewController {
let imageView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
imageView.frame = view.bounds
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
view.addSubview(imageView)
}
func integrate(image: UIImage){
imageView.image = image
}
}
答
self.present(BViewController(), animated: false, completion: { Void in
BViewController().integrate(image: imageSaved)
})
短语BViewController()
指 “建立一个完全新的视图控制器”。但你说了两遍!所以你呈现的视图控制器(第一行)和你给图像的视图控制器(第二行)是两个完全不同的视图控制器。
答
我终于与马特的帮助下固定它:
class AViewController: UIViewController {
func capture(){
...
let destinationVC = BViewController()
destinationVC.image = imageSaved
self.present(destinationVC, animated: false, completion: nil)
}
}
//=====================================================
class BViewController : UIViewController {
var image = UIImage()
let imageView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
imageView.frame = view.bounds
imageView.image = image
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
view.addSubview(imageView)
}
}
是的,你说得对!我修复了它并发布了我的解决方案。谢谢 ! – TheTurtle