Activity indicator starts before function is called

324 views Asked by At

I have a button that calls this IBAction function. The activity indicator starts as soon as the app runs instead of when the button is tapped, disappears as expected when the function completes its work then never appears again. This seems like a really simple interface thing but I'm stumped. Any ideas?

 @IBAction func saveToCamera() {

    // Start the spinner
    activityIndicator.startAnimating()

    //Create the UIImage
    UIGraphicsBeginImageContext(canvas.frame.size)
    canvas.layer.renderInContext(UIGraphicsGetCurrentContext())
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    //Save it to the camera roll
    UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:", nil)



}

func image(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo:UnsafePointer<Void>) {
     activityIndicator.stopAnimating()
        if error != nil {
             println(error) }
    }
1

There are 1 answers

3
Duncan C On BEST ANSWER

It sounds like you have the isAnimating property on the activity view set to true in your storyboard. make sure it's false, and that the hidesWhenStopped property is true.

Then your code won't work as posted. If you want the spinner to stop animating when the call to UIImageWriteToSavedPhotosAlbum is complete, you need to provide a completionTarget and completionSelector. Something like this:

(Edited to use correct method signature for completion method)

UIImageWriteToSavedPhotosAlbum(
  image, 
  self, 
  "image:didFinishSavingWithError:contextInfo:", 
  nil)

And a method to be called once the write is complete:

func image(image: UIImage, 
  didFinishSavingWithError 
  error: NSErrorPointer, 
  contextInfo: UnsafePointer<Void>) 
  {
     activityIndicator.stopAnimating()
     if error != nil 
     {
       println(error) 
     }
  }