0

I have a save image button which is used to save an image to the photo library after a new image is taken, or an existing photo is imported and modified inside the app.

However, if the save image button is pressed without an image taken or loaded into the app, it hangs and becomes unresponsive due to it not being able to save an image.

Any advice on what I would need to stop the button from functioning until an image is loaded?

//Save Image Button Function
@IBAction func saveImage(_ sender: AnyObject) {
    let imageData = UIImageJPEGRepresentation(photoViewer.image!, 1.0)
    let compressedJPGImage = UIImage(data: imageData!)
    UIImageWriteToSavedPhotosAlbum(compressedJPGImage!, nil, nil, nil)
    let alert = UIAlertView(title: "Saved!",
                            message: "Your image has been saved to Photo Library!",
                            delegate: nil,
                            cancelButtonTitle: "Ok")
    alert.show()
DSpooner
  • 13
  • 3
  • This post may have your answer: http://stackoverflow.com/questions/25524638/disable-a-button-on-swift If you post some code we can probably give you a more specific solution. – Nicko Jul 23 '16 at 14:25
  • Hi Nicko thanks for responding. I don't actually want to disable the button outright, as it will be needed later on. This is the code I have for this particular button: //Save Image Button Function @IBAction func saveImage(_ sender: AnyObject) { let imageData = UIImageJPEGRepresentation(photoViewer.image!, 1.0) let compressedJPGImage = UIImage(data: imageData!) UIImageWriteToSavedPhotosAlbum(compressedJPGImage!, nil, nil, nil) let alert = UIAlertView(title: "Saved!", message: "Your image has been saved to Photo Library!", delegate: nil, cancelButtonTitle: "Ok") alert.show() – DSpooner Jul 23 '16 at 15:22
  • Hi DSpooner, that's ok, the button can still be re-enabled at any point by setting the property to `true` again. Check the response from @MShahmeer below, they have pointed out where to initialize it to `false` and where to set it to `true` again. – Nicko Jul 23 '16 at 18:16

1 Answers1

0

First, in viewDidLoad, disable the button from being pressed by:

button.enabled = false

Then, use a conditional if statement where it is checked whether an image has been loaded into the app. If it has, re enable the button by:

button.enabled = true
MShahmeer
  • 159
  • 5