@@ -224,20 +224,7 @@ class MultipleImagePicker: NSObject, TLPhotosPickerViewControllerDelegate, UINav
224224 // add loading view
225225 let alert = UIAlertController ( title: nil , message: " Please wait... " , preferredStyle: . alert)
226226
227- let loadingIndicator = UIActivityIndicatorView ( frame: CGRect ( x: 10 , y: 5 , width: 50 , height: 50 ) )
228-
229- loadingIndicator. hidesWhenStopped = true
230- loadingIndicator. style = UIActivityIndicatorView . Style. gray
231-
232- if #available( iOS 13 . 0 , * ) {
233- loadingIndicator. color = . secondaryLabel
234- } else {
235- loadingIndicator. color = . black
236- }
237-
238- loadingIndicator. startAnimating ( )
239-
240- alert. view. addSubview ( loadingIndicator)
227+ alert. showLoading ( )
241228
242229 // handle controller
243230 self . getTopMostViewController ( ) ? . present ( alert, animated: true , completion: {
@@ -330,27 +317,58 @@ extension MultipleImagePicker: TLPhotosPickerLogDelegate {
330317// CropViewControllerDelegate
331318extension MultipleImagePicker : CropViewControllerDelegate {
332319 func cropViewController( _ cropViewController: CropViewController , didCropToImage image: UIImage , withRect cropRect: CGRect , angle: Int ) {
333- let filePath = getImagePathFromUIImage ( uiImage: image, prefix: " crop " )
334- let TLAsset = self . selectedAssets. first
320+ guard
321+ let TLAsset = self . selectedAssets. first,
322+ let filePath = getImagePathFromUIImage ( uiImage: image, prefix: " crop " )
323+ else {
324+ self . dismissComplete ( )
325+ return
326+ }
335327
336328 // Dismiss twice for crop controller & picker controller
337329 DispatchQueue . main. async {
338- self . getTopMostViewController ( ) ? . dismiss ( animated: true , completion: {
339- self . getTopMostViewController ( ) ? . dismiss ( animated: true , completion: {
340- if filePath != " " , TLAsset != nil {
341- self . fetchAsset ( TLAsset: TLAsset!) { object in
330+ cropViewController. dismiss ( animated: true ) {
331+ let alert = UIAlertController ( title: nil , message: " Please wait... " , preferredStyle: . alert)
332+
333+ alert. showLoading ( )
334+
335+ self . getTopMostViewController ( ) ? . present ( alert, animated: true ) {
336+ self . fetchAsset ( TLAsset: TLAsset) { object in
342337
343- object. data![ " crop " ] = [
344- " height " : image. size. height,
345- " width " : image. size. width,
346- " path " : filePath,
347- ]
338+ object. data![ " crop " ] = [
339+ " height " : image. size. height,
340+ " width " : image. size. width,
341+ " path " : filePath,
342+ ]
348343
344+ DispatchQueue . main. async {
349345 self . resolve ( [ object. data] )
346+ alert. dismiss ( animated: true ) {
347+ self . getTopMostViewController ( ) ? . dismiss ( animated: true )
348+ }
350349 }
351350 }
352- } )
353- } )
351+ }
352+ }
353+ }
354+ }
355+ }
356+
357+ extension UIAlertController {
358+ func showLoading( ) {
359+ let loadingIndicator = UIActivityIndicatorView ( frame: CGRect ( x: 10 , y: 5 , width: 50 , height: 50 ) )
360+
361+ loadingIndicator. hidesWhenStopped = true
362+ loadingIndicator. style = UIActivityIndicatorView . Style. gray
363+
364+ if #available( iOS 13 . 0 , * ) {
365+ loadingIndicator. color = . secondaryLabel
366+ } else {
367+ loadingIndicator. color = . black
354368 }
369+
370+ loadingIndicator. startAnimating ( )
371+
372+ self . view. addSubview ( loadingIndicator)
355373 }
356374}
0 commit comments