gpt4 book ai didi

swift - 拍照传给不同的UIViewController Swift 3.0

转载 作者:行者123 更新时间:2023-11-28 16:03:15 25 4
gpt4 key购买 nike

我正在尝试在我的应用程序中拍摄图像,以便将其保存到我的设备并将其传递给下一个要预览的 View Controller 。我看到人们这样做的方式是将他们拍摄的图像存储在 uiimage 中。然后在 prepareforsegue 期间,他们将目标 View Controller 中的 uiimage 变量设置为您在前一个 View Controller 中拍摄的照片。在 dest View Controller 中,我看到人们按如下方式显示图像:imageName.image = imageVariable。当我将变量传递给目标 View Controller 并尝试在下一个 View Controller 中显示它时,它显示为 nil 值。我哪里错了?

第一个 View Controller :

 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)
})

}


}

第二个 View Controller :

 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

}

最佳答案

您需要将 viewController 插入 block 内。实际上,此代码中发生的事情是在 prepareForSegue 之后调用完成 block 。所以你的形象总是“零”。

尝试像这样推送 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)
})
}

希望它能帮到你..快乐编码!!

关于swift - 拍照传给不同的UIViewController Swift 3.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40687457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com