简体   繁体   中英

Activity indicator starts before function is called

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) }
    }

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) 
     }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM