如何将图像从 camera/camera 滚动到 chosen/taken 后的新视图?
How to pass an image from camera/camera roll to a new view after it has been chosen/taken?
我已经在我的应用程序中添加了一个相机和照片库,但我想要做的是当一些人选择相机、拍照并选择它时,我希望它能将用户引导到一个新的viewcontroller 并在那里显示图像而不是在按钮所在的当前视图上?
我在 Google 上进行了搜索,但没有在 swift 中找到任何明确的内容,而且由于我不了解 ObjC,所以我不确定我所看到的是否正确!
下面是我当前的代码:
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 imageLoad = UIImagePickerController()
imageLoad.delegate = self
imageLoad.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imageLoad.allowsEditing = false
self.presentViewController(imageLoad, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image:UIImage, editingInfo: [String : AnyObject]?) {
self.dismissViewControllerAnimated(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
svc.image = image
self.presentViewController(svc, animated: true, completion: nil)
}
然后,在 imageViewerController 中:
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
}
}
根据上面的回答,你试图将图像设置为图像视图,当你应该尝试将图像设置为图像视图的图像时,所以,它应该是:
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
}
}
我已经在我的应用程序中添加了一个相机和照片库,但我想要做的是当一些人选择相机、拍照并选择它时,我希望它能将用户引导到一个新的viewcontroller 并在那里显示图像而不是在按钮所在的当前视图上?
我在 Google 上进行了搜索,但没有在 swift 中找到任何明确的内容,而且由于我不了解 ObjC,所以我不确定我所看到的是否正确!
下面是我当前的代码:
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 imageLoad = UIImagePickerController()
imageLoad.delegate = self
imageLoad.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
imageLoad.allowsEditing = false
self.presentViewController(imageLoad, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image:UIImage, editingInfo: [String : AnyObject]?) {
self.dismissViewControllerAnimated(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
svc.image = image
self.presentViewController(svc, animated: true, completion: nil)
}
然后,在 imageViewerController 中:
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
}
}
根据上面的回答,你试图将图像设置为图像视图,当你应该尝试将图像设置为图像视图的图像时,所以,它应该是:
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
}
}