我正在尝试在我的应用中拍摄图像,以便将其保存到我的设备并将其传递给下一个要预览的视图控制器。我看到人们这样做的方式是将他们拍摄的图像存储在uiimage中。然后在prepareforsegue期间,他们将目标视图控制器中的uiimage变量设置为您在上一个视图控制器中拍摄的照片。从那里在dest视图控制器中,我看到人们显示图像如下:imageName.image = imageVariable。当我将变量传递给目标视图控制器并尝试在下一个视图控制器中显示它时,它显示为零值。我哪里错了?
First ViewController:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ToDetailPage" {
let nextScene = segue.destination as! PostDetailPageViewController
nextScene.itemImage = self.image
// nextScene?.myimg.image = self.image
}
}
@IBAction func TakePhotoButtonClicked(_ sender: AnyObject) {
if let videoConnection = sessionOutput.connection(withMediaType: AVMediaTypeVideo){
sessionOutput.captureStillImageAsynchronously(from: videoConnection, completionHandler: {
buffer, error in
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)
self.image = UIImage(data: imageData!)
UIImageWriteToSavedPhotosAlbum(UIImage(data: imageData!)!, nil, nil, nil)
})
}
}
第二个ViewController:
var itemImage: UIImage!
@IBOutlet weak var myimg: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
self.categories.dataSource = self;
self.categories.delegate = self;
setUpMap()
myimg.image = itemImage
}
答案 0 :(得分:1)
您需要在块内推送viewController。实际上,在这段代码中发生的事情是在prepareForSegue之后调用完成块。所以你的形象总是“没有”。
尝试像这样推送viewController:
if let videoConnection = sessionOutput.connection(withMediaType: AVMediaTypeVideo){
sessionOutput.captureStillImageAsynchronously(from: videoConnection, completionHandler: {
buffer, error in
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(buffer)
self.image = UIImage(data: imageData!)
UIImageWriteToSavedPhotosAlbum(UIImage(data: imageData!)!, nil, nil, nil)
// push view controller here
let destinationVC = SecondViewController()
destinationVC.image = self.image
self.navigationController.pushViewController(destinationVC, animated: true)
})
}
希望它会帮助你..快乐编码!!