我添加了相机和相机照片库到我的应用程序,但我想要做的是当有人选择相机,拍照并选择它时,我希望它将用户引导到新的viewcontroller并在那里显示图像而不是当前视图按钮在哪里?
到目前为止,在谷歌搜索时,我已经设法在相机或图书馆被解雇时加载新视图,但现在我似乎无法弄清楚如何导入照片也是新视图?!
如果有人可以提供帮助,那就太好了。
下面是我目前的代码:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func Camera(sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.Camera;
imagePicker.allowsEditing = false
self.presentViewController(imagePicker, animated: true, completion: nil)
}
}
@IBAction func Images(sender: AnyObject) {
let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
image.allowsEditing = false
self.presentViewController(image, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image:UIImage, editingInfo: [String : AnyObject]?) {
self.dismissViewControllerAnimated(true, completion: nil)
let svc = self.storyboard!.instantiateViewControllerWithIdentifier("imageViewer") as! ImageViewController
self.presentViewController(svc, animated: true, completion: nil)
}
}
答案 0 :(得分:0)
向视图控制器添加可选属性
class ImageViewController: UIViewController {
var image : UIImage? = nil
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image:UIImage, editingInfo: [String : AnyObject]?) {
self.dismissViewControllerAnimated(true, completion: nil)
let svc = self.storyboard!.instantiateViewControllerWithIdentifier("imageViewer") as! ImageViewController
svc.image = image // here
self.presentViewController(svc, animated: true, completion: nil)
}
答案 1 :(得分:0)
基于上面的答案,你试图将图像设置为图像视图,当你应该尝试将图像设置为图像视图的图像时,它应该是:
func viewDidLoad(){ super.viewDidLoad()
// Because the self.image is an optional var, you need to unwrap it
if let image = self.image {
self.imageView.image = self.image
}
}
答案 2 :(得分:0)
在ImageViewController
或其他接收的ViewController中,添加:
var selectedImage: UIImage? = nil
在didFinishPickingImage
中,将代码替换为:
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image:UIImage, editingInfo: [String : AnyObject]?) {
self.dismissViewControllerAnimated(true, completion: nil)
let svc = self.storyboard!.instantiateViewControllerWithIdentifier("imageViewer") as! ImageViewController
svc.selectedImage = image
self.presentViewController(svc, animated: true, completion: nil)
}
在svc
所指的接收ViewController中,添加一个显示图像的方法:
class ImageViewController: UIViewController {
var selectedImage: UIImage? = nil
var yourImageView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
...
yourImageView.frame = view.frame
yourImageView.image = selectedImage
}
...
}
答案 3 :(得分:0)
我想建议一点修改。
请看一下这个答案:Presenting a modal view controller immediately after dismissing another
func presentImageViewerWithImage(image: UIImage) {
let svc = self.storyboard!.instantiateViewControllerWithIdentifier("imageViewer") as! ImageViewController
svc.image = image //Need to make an UIImage instance variable in new viewController
self.presentViewController(svc, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image:UIImage, editingInfo: [String : AnyObject]?) {
self.dismissViewControllerAnimated(false, completion: {
self.presentImageViewerWithImage(image)
})
}
然后在新视图控制器中创建UIImageView
IBOutlet
并将选定的图像表单选择器显示到' ImageView'
@IBOutlet weak var aImageView: UIImageView!
var image : UIImage?
override func viewDidLoad() {
super.viewDidLoad()
if let image = image {
self.aImageView.image = image
}
}