我是2 VC:
点按时第一个VC 带1个按钮>> 2个选项张贴图像形式凸轮或图书馆
第二个VC 选择的图像表格VC1被伪装到第二个VC
我试图想出这个错误没有任何成功
致命错误:在展开Optional值时意外发现nil 指着
imageView.image=info[UIImagePickerControllerOriginalImage] as? UIImage
我在dropbox https://www.dropbox.com/s/w7blbnyac9qyxgw/segImage.zip?dl=0
中上传了该项目VC1
class ViewController:UIViewController,UIAlertViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIPopoverControllerDelegate
{
@IBOutlet weak var btnClickMe: UIButton!
@IBOutlet weak var imageView: UIImageView!
var picker:UIImagePickerController?=UIImagePickerController()
var popover:UIPopoverController?=nil
override func viewDidLoad()
{
super.viewDidLoad()
picker!.delegate=self
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "" ) {
let destVC : second = segue.destinationViewController as! second
destVC.pics = imageView.image!
}
}
@IBAction func btnImagePickerClicked(sender: AnyObject)
{
let alert:UIAlertController=UIAlertController(title: "Choose Image", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet)
let cameraAction = UIAlertAction(title: "Camera", style: UIAlertActionStyle.Default)
{
UIAlertAction in
self.openCamera()
}
let gallaryAction = UIAlertAction(title: "Gallary", style: UIAlertActionStyle.Default)
{
UIAlertAction in
self.openGallary()
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel)
{
UIAlertAction in
}
// Add the actions
picker?.delegate = self
alert.addAction(cameraAction)
alert.addAction(gallaryAction)
alert.addAction(cancelAction)
// Present the controller
if UIDevice.currentDevice().userInterfaceIdiom == .Phone
{
self.presentViewController(alert, animated: true, completion: nil)
}
else
{
popover=UIPopoverController(contentViewController: alert)
popover!.presentPopoverFromRect(btnClickMe.frame, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
}
}
func openCamera()
{
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
{
picker!.sourceType = UIImagePickerControllerSourceType.Camera
self .presentViewController(picker!, animated: true, completion: nil)
}
else
{
openGallary()
}
}
func openGallary()
{
picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
if UIDevice.currentDevice().userInterfaceIdiom == .Phone
{
self.presentViewController(picker!, animated: true, completion: nil)
}
else
{
popover=UIPopoverController(contentViewController: picker!)
popover!.presentPopoverFromRect(btnClickMe.frame, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
picker .dismissViewControllerAnimated(true, completion: nil)
imageView.image=info[UIImagePickerControllerOriginalImage] as? UIImage
}
func imagePickerControllerDidCancel(picker: UIImagePickerController)
{
print("picker cancel.")
}
}
第二个VC
class second: UIViewController {
var pics = UIImage()
@IBOutlet var image: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
if let img = UIImage(named: "pics")
{
self.image.image = img
}
}
答案 0 :(得分:1)
您的故事板布线错误。
应该在second
的实例而不是ViewController.
的实例上设置图片。请注意,崩溃发生在ViewController
而不是second
此代码:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
picker .dismissViewControllerAnimated(true, completion: nil)
imageView.image=info[UIImagePickerControllerOriginalImage] as? UIImage
}
正在尝试在imageView.image
中设置ViewController
。如果您查看故事板,则UIImageView
中没有ViewController
。
您在imageView
上定义ViewController
的方式是导致此次崩溃的原因:
@IBOutlet weak var imageView: UIImageView!
在那里!
,你说,"这永远不会是零。"嗯,显然不是这种情况,因为因为这个IBOutlet没有连接到UIImageView
,所以它是零。因此,当你试图访问它时,BOOM!
解决此问题:
second
选择second
' s image.imageView
属性其他建议: - 在命名中使用一致性,例如second vs SecondViewController