2015-10-19 184 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