2016-08-11 28 views
1

我實現了我的應用程序這個指標:只要https://github.com/vincechan/SwiftLoadingIndicator右後查看沒有時間之前出現耗時的操作

這種負載微調來前,我希望它旋轉的時候,爲我的操作持續。

在我的操作中,我在圖像上做了一些工作,然後將它們展示給用戶,但每次點擊「運行操作」按鈕後,應用程序凍結幾秒鐘,更新視圖並顯示覆蓋圖微調。 我已經試過異步分派是這樣的:

@IBAction func manipulateImage(sender: AnyObject) { 
    dispatch_async(dispatch_get_main_queue(), { 
        LoadingOverlayView.show() 
       }); 
     if let beginImage = CIImage(image: self.imageView.image!) { 
        var outputImage = OutputImage(sourceImage: beginImage) 
    //apply CIFilters: 
        outputImage.applyFilter(FilterType.Grayscale) 
        outputImage.applyFilter(FilterType.Sepia) 
        outputImage.applyFilter(FilterType.Vignette) 
        outputImage.applyFilter(FilterType.Shadow) 

        let cgimg = self.imageContext.createCGImage(outputImage, fromRect: outputImage.extent) 

        self.imageView.image = UIImage(CGImage: cgimg) 
    LoadingOverlayView.hide() 
} 

,但它不工作。同樣正常的方法調用,沒有異步調度,行爲完全相同。

+1

您將需要顯示比此更多的代碼。這段代碼在哪裏被調用?你在哪裏隱藏'LoadingOverlayView'? – AdamPro13

+0

@ AdamPro13我已經更新了這篇文章,裏面有很多東西。 – user2462794

+0

你在主線程上運行它嗎? – Wain

回答

1

您可能在UI線程上運行此代碼。如果是這樣,您可以按如下方式將操作調度到後臺隊列:

// we're already on the UI thread, so dispatch to a background queue 
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    // do your background operations ... 
    if let beginImage = CIImage(image: self.imageView.image!) { 
     var outputImage = OutputImage(sourceImage: beginImage) 
     //apply CIFilters: 
     outputImage.applyFilter(FilterType.Grayscale) 
     outputImage.applyFilter(FilterType.Sepia) 
     outputImage.applyFilter(FilterType.Vignette) 
     outputImage.applyFilter(FilterType.Shadow) 

     let cgimg = self.imageContext.createCGImage(outputImage, fromRect: outputImage.extent) 

     // make sure to dispatch UI function back to main queue! 
     dispatch_async(dispatch_get_main_queue(), { 
       self.imageView.image = UIImage(CGImage: cgimg) 

       LoadingOverlayView.hide() 
      }); 
    } 
}); 

// we're already on the UI thread, so show the overlay now 
LoadingOverlayView.show() 
+0

這幾乎是我想要的,但有這個日誌:'這個應用程序正在從後臺線程修改自動佈局引擎,這可能導致引擎損壞和奇怪的崩潰。這將在未來的版本中引發異常。「我想我們應該擺脫它。 – user2462794

+0

您被警告在後臺線程上執行UI操作時,請注意此警告。請參閱代碼中關於將UI操作分派回主隊列的說明(例如,您的覆蓋隱藏操作)。 – CSmith

相關問題