我正在尝试在ios应用上制作图片上传器。
编辑7
现在更清楚地问这个问题:Present Modal for Image Select Erases UI
编辑结束7
我要做的第一件事是让用户选择他们想要上传的图片。
编辑6
我已将所有代码简化为只按一个按钮。模态演示文稿删除了上一页的UI。我如何防止这种情况。
import UIKit
class AddImageToStandViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func loadImageButtonTapped(sender: UIButton) {
let imagePicker = UIImagePickerController()
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
我发现选择工作正常,如果我从另一个控制器推送/ segue到这个控制器,则不会破坏UI。
编辑结束6
下面是我在此之前尝试的其他方法
到目前为止,此代码允许用户按下按钮并从照片中选择照片。但是,在他们点击照片之后,它只会将它们返回到空视图,但底部的导航栏除外。
似乎我的用户正在被退回,而不是他们来自的视图,而是一个单独的空视图。如何将用户返回到他们来自的视图?
import UIKit
class CreateStandViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet var image: UIImageView!
...
@IBAction func selectPicture(sender: AnyObject) {
let ImagePicker = UIImagePickerController()
ImagePicker.delegate = self
ImagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(ImagePicker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
image.image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismissViewControllerAnimated(true, completion: nil)
}
...
请告诉我是否还有其他信息。
编辑1
此代码具有相同的效果
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
image.image = info[UIImagePickerControllerOriginalImage] as? UIImage
picker.dismissViewControllerAnimated(true, completion: nil)
}
我尝试在下方添加此行,但收到错误消息,指出CreateStandViewController.Type
无法转换为预期的UIViewController
。
self.presentViewController(CreateStandViewController, animated: true, completion: nil)
编辑2
下面这段代码没有错误但也没有改变
let createStandController = CreateStandViewController()
self.presentViewController(createStandController, animated: true, completion: nil)
编辑3
我从字面上复制了所有代码:http://www.codingexplorer.com/choosing-images-with-uiimagepickercontroller-in-swift/现在有了:
import UIKit
class CreateStandViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet var imageView: UIImageView!
let imagePicker = UIImagePickerController()
@IBAction func loadImageButtonTapped(sender: UIButton) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .PhotoLibrary
presentViewController(imagePicker, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
}
// MARK: - UIImagePickerControllerDelegate Methods
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageView.contentMode = .ScaleAspectFit
imageView.image = pickedImage
}
dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
}
这与以前的问题相同。选择图像后,我会回到底部带有导航栏的白色视图。
编辑4
我在自己独立的项目中运行相同的代码作为独立的视图控制器,它的工作原理。我的应用程序背景中有一些代码可能会破坏代码的功能(可能)。我猜这可能与导航栏有关吗?
编辑5
我删除了底部的标签栏,通过当前的Modally segue连接到图像选择视图,但它没有效果。
答案 0 :(得分:0)
只需更换
即可self.dismissViewControllerAnimated(true, completion: nil)
带
picker.dismissViewControllerAnimated(true, completion: nil)
我认为你会明白这一点