活动指示器在调用函数之前启动

时间:2015-06-16 23:05:42

标签: ios swift xcode6 uiactivityindicatorview

我有一个调用此IBAction功能的按钮。活动指示器会在应用程序运行时立即启动,而不是在按下按钮时启动,当功能完成其工作然后再也不会再次出现时,按预期消失。这看起来像一个非常简单的界面,但我很难过。有什么想法吗?

 @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 个答案:

答案 0 :(得分:3)

听起来您在故事板中将活动视图上的isAnimating属性设置为true。确保它是错误的,并且hidesWhenStopped属性为真。

然后您的代码将无法发布。如果您希望微调器在完成对UIImageWriteToSavedPhotosAlbum的调用后停止制作动画,则需要提供completionTargetcompletionSelector。像这样:

(编辑使用正确的方法签名完成方法)

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

写入完成后要调用的方法:

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