在我正在开发的应用程序中,我有一个图像选择器,通过单击视图控制器1上的按钮可以调用该图像选择器。选择图像后,我要将其发送到视图控制器2,以便用户可以输入有关上传之前的图片。
在下面的代码中,我可以正常工作,但是有一件事我不知道。当我选择查看控制器2时,第一个视图控制器始终闪烁到屏幕上,而不是立即选择查看控制器2。 这一系列事件似乎是图像选择器被驳回->视图控制器1短暂出现->准备进行筛选->然后视图控制器2加载。
是否可以更改代码,以便在选择图像后立即进行查看控制器2的选择?如果由于取消了图像选择器而未选择图像,我想返回视图控制器1。
import UIKit
import Parse
class UserHome: UIViewController {
var newImage: UIImage?
@IBOutlet weak var welcomeLabel: UILabel!
@IBOutlet weak var imageArea: UIImageView!
@IBOutlet weak var currentUser: UIButton!
@IBAction func tapLogOut(_ sender: Any) {
PFUser.logOut()
}
@IBAction func tapWeddingInfo(_ sender: Any) {
opeURLFromString(urlString: weddingURL)
}
@IBAction func tapSlideShow(_ sender: Any) {
self.performSegue(withIdentifier: "showSlideShow", sender: self)
}
@IBAction func tapAddImage(_ sender: Any) {
print("tapped add image")
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .photoLibrary
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
loadPage()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showImageSubmit"{
let vc = segue.destination as! ImageSubmit
vc.newImage = newImage
}
}
func loadPage() {
print("query a database for images")
print("load an array with those images")
// Figure out a way to have images scroll into viewing area
if let userEmail = PFUser.current()?.username {
currentUser.setTitle(userEmail, for: [])
}
imageArea.image = UIImage(named: userHomeImage)
welcomeLabel.text = userHomeMsg
print ("loaded User Home")
}
}
extension UserHome: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
// Convert image into PFFile data type
// self.imageArea.image = image
newImage = image
self.dismiss(animated: true, completion: nil)
}
self.performSegue(withIdentifier: "showImageSubmit", sender: self)
}
}
答案 0 :(得分:1)
您可以使用completion
方法中的dismiss
。撤消UIImagePickerController
之后,下一个控制器出现
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
guard let image = info[.originalImage] as? UIImage else {
// dismiss(animated: true)
return
}
// Convert image into PFFile data type
// self.imageArea.image = image
newImage = image
dismiss(animated: true) {
self.performSegue(withIdentifier: "showImageSubmit", sender: self)
}
}