2015-06-16 23 views
0

我有一個調用此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) } 
    } 
+0

什麼屬性有您設置故事板場景中的活動指示器? – Paulw11

+0

你有更大的問題 - 你的問題是理解同步和異步編程。您已將所有代碼放在一個函數中,最後一行是stopAnimating(),這意味着在您調用此函數之後,動畫師將永遠不會顯示,因爲您正在啓動它,但稍後您將停止它,因此它永遠不會顯示給用戶。鄧肯已經展示瞭如何讓它停止。確保你明白爲什麼你所做的不能工作。 – Gruntcakes

回答

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

它解決了問題,雖然應用程序現在崩潰時輕按按鈕。 Gruntcakes有一點。我看到我正在啓動並停止它的功能。我將它設置爲IB的動畫,這意味着'不是靜態圖像'。沒有意識到這是我設定的默認狀態。沒有選中。我根據@Duncan-C的建議進行了上述編輯。 – OsakaStarbux

+0

在什麼行上崩潰,什麼是錯誤? –

+0

錯誤是'NSInvalidArgumentException',原因:' - [NSInvocation setArgument:atIndex:]:index(3)outbound bound [-1,2]'。我能夠從文章「訪問Swift中訪問iOS 8相機和照片庫 - Techotopia」中的上述代碼修復它「 – OsakaStarbux