Swift segue 在 ImagePickerController 的 didFinishPickingMediaWithInfo 中不工作
Swift segue not working in ImagePickerController's didFinishPickingMediaWithInfo
我有这个代码:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
//dismiss(animated: true, completion: nil)
guard info[UIImagePickerControllerMediaType] != nil else { return }
let mediaType = info[UIImagePickerControllerMediaType] as! CFString
print("video is",mediaType)
switch mediaType {
case kUTTypeImage:
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage, let pickedAsset = info[UIImagePickerControllerPHAsset] as? PHAsset {
self.screenshotOut = pickedImage
MyVariables.isScreenshot = true
self.thumbnailImage = pickedImage
let creationDate = pickedAsset.creationDate
self.mediaDate = creationDate
dismiss(animated: true, completion: nil)
self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
}
}
break
....
default:
dismiss(animated: true, completion: nil)
print("something else")
break
}
}
如您所见,它一直在等待我的 imagePickerController 拥有照片资产。然后它设置一些 class 变量并执行 segue。我有这个功能来准备 segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "CreatePost_Segue" {
guard self.thumbnailImage != nil else {
return
}
if MyVariables.isScreenshot == true {
guard self.screenshotOut != nil else {
return
}
//self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
} else {
guard self.thumbnailImage != nil else {
return
}
//self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
//now I set those three varibles?
}
let controller = segue.destination as! CreatePostViewController
controller.postDate = mediaDate
controller.thumbnailImage = thumbnailImage
controller.videoURL = videoURL
controller.screenshotOut = screenshotOut
print("segued",controller.postDate)
print("segued",controller.thumbnailImage)
print("segued",controller.videoURL)
print("segued",controller.screenshotOut)
}
}
它打印:
segued Optional(2016-05-04 17:48:53 +0000)
segued Optional(<UIImage: 0x13d365720> size {2448, 3264} orientation 0 scale 1.000000)
segued nil
segued Optional(<UIImage: 0x13d365720> size {2448, 3264} orientation 0 scale 1.000000)
所以看起来所有的控制器分配都有效...视频 URL 是故意为零并且它是一个可选的所以我相信没关系。
但是 segue 永远不会发生。选择器控制器被关闭了,这很好,但为什么这不会继续呢?
因为 dismiss 是动画的,VC 当前正在呈现 camera/photo libaray,所以它不能呈现另一个,你可以调度它
dismiss(animated: true, completion: nil)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
}
或以获得正确的延迟
self.dismiss(animated: true) {
self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
}
// 您可能会寻找类似 VC 的消息 // 在控制台
我有这个代码:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
//dismiss(animated: true, completion: nil)
guard info[UIImagePickerControllerMediaType] != nil else { return }
let mediaType = info[UIImagePickerControllerMediaType] as! CFString
print("video is",mediaType)
switch mediaType {
case kUTTypeImage:
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage, let pickedAsset = info[UIImagePickerControllerPHAsset] as? PHAsset {
self.screenshotOut = pickedImage
MyVariables.isScreenshot = true
self.thumbnailImage = pickedImage
let creationDate = pickedAsset.creationDate
self.mediaDate = creationDate
dismiss(animated: true, completion: nil)
self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
}
}
break
....
default:
dismiss(animated: true, completion: nil)
print("something else")
break
}
}
如您所见,它一直在等待我的 imagePickerController 拥有照片资产。然后它设置一些 class 变量并执行 segue。我有这个功能来准备 segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?){
if segue.identifier == "CreatePost_Segue" {
guard self.thumbnailImage != nil else {
return
}
if MyVariables.isScreenshot == true {
guard self.screenshotOut != nil else {
return
}
//self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
} else {
guard self.thumbnailImage != nil else {
return
}
//self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
//now I set those three varibles?
}
let controller = segue.destination as! CreatePostViewController
controller.postDate = mediaDate
controller.thumbnailImage = thumbnailImage
controller.videoURL = videoURL
controller.screenshotOut = screenshotOut
print("segued",controller.postDate)
print("segued",controller.thumbnailImage)
print("segued",controller.videoURL)
print("segued",controller.screenshotOut)
}
}
它打印:
segued Optional(2016-05-04 17:48:53 +0000)
segued Optional(<UIImage: 0x13d365720> size {2448, 3264} orientation 0 scale 1.000000)
segued nil
segued Optional(<UIImage: 0x13d365720> size {2448, 3264} orientation 0 scale 1.000000)
所以看起来所有的控制器分配都有效...视频 URL 是故意为零并且它是一个可选的所以我相信没关系。
但是 segue 永远不会发生。选择器控制器被关闭了,这很好,但为什么这不会继续呢?
因为 dismiss 是动画的,VC 当前正在呈现 camera/photo libaray,所以它不能呈现另一个,你可以调度它
dismiss(animated: true, completion: nil)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
}
或以获得正确的延迟
self.dismiss(animated: true) {
self.performSegue(withIdentifier: "CreatePost_Segue", sender: nil)
}
// 您可能会寻找类似 VC 的消息 // 在控制台