检查imageView是否为空

时间:2015-10-19 04:07:06

标签: ios swift

我的应用程序中有一个相机功能,当你拍照时,它会将图片放入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)
}

2 个答案:

答案 0 :(得分:4)

尝试将其放在viewDidAppear方法中。应该这样做。

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

答案 1 :(得分: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
    })
}