2015-10-19 217 views
1

我在我的应用程序中有一个摄像头功能,当您拍摄图片时,它将该图片放入imageView。我有一个隐藏的按钮,当图像放置在imageView中时,我想要的是按钮被隐藏。检查imageView是否为空

@IBOutlet weak var imageView: UIImageView! 
@IBOutlet weak var toGoFurther: UIButton! 

override func viewDidLoad() { 
    super.viewDidLoad() 

    toGoFurther.hidden = true 

    if (self.imageView.image != nil){ 
     toGoFurther.hidden = false 
    } 

    let testObject = PFObject(className: "TestObject") 
    testObject["foo"] = "bar" 
    testObject.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in 
     print("Object has been saved.") 
    } 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

@IBAction func continueNextPage(sender: AnyObject) { 
} 

@IBAction func takePhoto(sender: AnyObject) { 
     if !UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){ 
      return 
     } 

     let imagePicker = UIImagePickerController() 
     imagePicker.delegate = self 
     imagePicker.sourceType = UIImagePickerControllerSourceType.Camera; 

     //Create camera overlay 
     let pickerFrame = CGRectMake(0, UIApplication.sharedApplication().statusBarFrame.size.height, imagePicker.view.bounds.width, imagePicker.view.bounds.height - imagePicker.navigationBar.bounds.size.height - imagePicker.toolbar.bounds.size.height) 
     let squareFrame = CGRectMake(pickerFrame.width/2 - 400/2, pickerFrame.height/2 - 400/2, 640, 640) 
     UIGraphicsBeginImageContext(pickerFrame.size) 

     let context = UIGraphicsGetCurrentContext() 
     CGContextSaveGState(context) 
     CGContextAddRect(context, CGContextGetClipBoundingBox(context)) 
     CGContextMoveToPoint(context, squareFrame.origin.x, squareFrame.origin.y) 
     CGContextAddLineToPoint(context, squareFrame.origin.x + squareFrame.width, squareFrame.origin.y) 
     CGContextAddLineToPoint(context, squareFrame.origin.x + squareFrame.width, squareFrame.origin.y + squareFrame.size.height) 
     CGContextAddLineToPoint(context, squareFrame.origin.x, squareFrame.origin.y + squareFrame.size.height) 
     CGContextAddLineToPoint(context, squareFrame.origin.x, squareFrame.origin.y) 
     CGContextEOClip(context) 
     CGContextMoveToPoint(context, pickerFrame.origin.x, pickerFrame.origin.y) 
     CGContextSetRGBFillColor(context, 0, 0, 0, 1) 
     CGContextFillRect(context, pickerFrame) 
     CGContextRestoreGState(context) 

     let overlayImage = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext(); 

     let overlayView = UIImageView(frame: pickerFrame) 
     overlayView.image = overlayImage 
     imagePicker.cameraOverlayView = overlayView 
     self.presentViewController(imagePicker, animated: true, completion: nil) 
    } 

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 
    imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage 
    dismissViewControllerAnimated(true, completion: nil) 
} 

回答

3

试着把这个放在viewDidAppear方法中。这应该做到这一点。

override func viewDidAppear(animated: Bool) { 
    super.viewDidAppear(animated)  
    toGoFurther.hidden = self.imageView.image == nil 
} 
+0

如果我添加动画:BOOL和动画怒吼咆哮,给人错误.......不,我试图只是如果else语句,并没有更改....我也通过使用打印语句检查,并没有得到一个打印 – RubberDucky4444

+1

嗯,你有错误将该代码放在奇怪的类文件。有什么错误? – mn1

+0

我输入了错误的内容。它现在的作品,非常感谢 – RubberDucky4444

0

因此,当图像被捕获并设置在视图中时,您想要取消隐藏按钮。你需要做这在您的didFinishPickingMediaWithInfo功能是这样的:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject: AnyObject]) { 
    imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage 

    // dissmiss the image picker controller window 
    self.dismissViewControllerAnimated(true, completion:^{ 
     toGoFurther.hidden = false 
    }) 
} 
+0

似乎还没有诀窍呢 – RubberDucky4444

+0

您能否在该完成块中打印一条语句以查看它是否正在执行。 – Abhinav