![](/img/trans.png)
[英]how to dismiss a popover and show an activity indicator while doing more processing
[英]How do you dismiss an activity indicator after processing time in Swift?
我有一個iOS Swift應用程序,它通過UIImagePicker使用相機和照片庫功能。 用戶可以從圖庫中選擇照片或拍照,然后將其添加到我的圖像陣列中。 但是,當用戶選擇要在選擇器中使用的照片或按“使用照片”按鈕時,我的應用程序將花費大約2秒鍾來處理該照片,並且選擇器直到那時才會消失。 因此,我想添加一個活動指示器,以使用戶知道該圖像已被選中並且正在處理中。 但是我不知道該怎么做。 我知道如何使用UIButtons顯示和隱藏活動指示器,但是在完成處理時間后如何隱藏活動指示器?
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
//Adds photo to image array
//Takes a very long time to process.
self.dismiss(animated: true, completion: nil)
}
對於用戶而言, 2秒會有點長,而對於iOS系統, 2秒會很長時間占據主線程。
無論您是否要顯示活動指示器,都不應在主線程中調用此類耗時的任務。 並且還需要在后台線程中執行此操作以正確更新UI元素(包括show / hide / animate UIActivityIndicatorView
)。
您的代碼將變成這樣:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
DispatchQueue.global(qos: .userInitiated).async {
DispatchQueue.main.async {
//UI updates needs to be done in the main thread
self.activityIndicator.startAnimating()
//Other UI updates & setups needed for mutual exclusion
}
//### In a background thread...
//Adds photo to image array
//Takes a very long time to process.
DispatchQueue.main.async {
//Reset mutual exclusion & restore UI state
self.activityIndicator.stopAnimating()
}
}
picker.dismiss(animated: true, completion: nil)
}
(假設在情節hidesWhenStopped
設置了UIActivityIndicatorView
的hidesWhenStopped
設置,並將其連接到@IBOutlet
activityIndicator
。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.