ios - 显示事件指示器,直到所有图像都成功加载

标签 ios iphone swift asynchronous uiimageview

我有一个包含五个 ImageView 的 collectionView 单元格。我正在使用以下 javimuu 给出的答案在后台加载图像。 Link

效果很好,所有图像都加载到正确的 ImageView 中。但这是一个非常缓慢的过程,并且图像以随机顺序加载。 当为特定单元格加载所有图像时,我很难显示事件指示器并隐藏它,即,在加载图像时继续显示指示器,并在加载所有 5 张图像时隐藏它。

请帮忙。

更新:

    func downloadImageFromLink(link:NSURL,group: dispatch_group_t, completion:(isDone: Bool) -> Void) {

    self.image = nil

    imageURL = link.absoluteString
    dispatch_group_enter(group)

    NSURLSession.sharedSession().dataTaskWithURL(link, completionHandler: {
        (data, response, error) -> Void in
        if error != nil {
            print(error)
            return
        }

        if let imageFromCache = imageCache.objectForKey(link.absoluteString) as? UIImage {

            self.image = imageFromCache
            dispatch_group_leave(group)
            return
        }
        dispatch_async(dispatch_get_main_queue(),{

            let imageToCache =  UIImage(data: data!)

            if self.imageURL == link.absoluteString {
                self.image = imageToCache
                imageCache.setObject(imageToCache!, forKey: link.absoluteString)
            }
            dispatch_group_leave(group)
        })
    }).resume()
}

func loadImagesInCell(cell: WatchFaceCell, images:NSArray ,completion:() ->Void) {
    self.startAnimating(cell.contentView)
    let group: dispatch_group_t = dispatch_group_create()
    for model in images{
        if model is WFCreatorModel {
            let wfModel: WFCreatorModel = model as! WFCreatorModel
            switch wfModel.imageType {
            case .Dial:
                cell.dialImage.downloadImageFromLink(wfModel.imageURL,group: group, completion: { (isDone) in
                })
            case .HourHand:
                cell.hourHandImage.downloadImageFromLink(wfModel.imageURL,group: group, completion: { (isDone) in
                })
            case .MinuteHand:
                cell.minuteHandImage.downloadImageFromLink(wfModel.imageURL,group: group, completion: { (isDone) in
                })
            case .SecondHand:
                cell.secondHandImage.downloadImageFromLink(wfModel.imageURL,group: group, completion: { (isDone) in
                })
            case .Notification:
                cell.notificationImage.downloadImageFromLink(wfModel.imageURL, group: group,completion: { (isDone) in
                })
            }//end of switch
        }//end of if else
    }//end of for
    dispatch_group_notify(group, dispatch_get_main_queue()) {
        self.stopAnimating(cell.contentView)
        completion()
    }
}

最佳答案

您将需要使用 DispatchGroup,您可以在发出异步请求时进入和离开。然后使用 .notify(queue:) 方法,该方法在组为“空”时触发。我能够找到与您正在寻找的代码非常相似的答案,因此我已将其链接到此处: Wait until swift for loop with asynchronous network requests finishes executing .

如果您还有其他问题,请告诉我。希望这对您有所帮助!

关于ios - 显示事件指示器,直到所有图像都成功加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42884718/

相关文章:

ios - 如何从 iOS 8 中的 SOAP 获取数据。

iphone - 令人困惑的闭包和完成句柄

iOS:调用原生录音机

iphone - 将 NSString 中的 ""字符替换为 "\"(以创建 Unix 路径)

swift - MapBox IOS SDK swift : how to show the user annotations from specific zoomLevel without delete and add again?

ios - 我将如何正确格式化此 Swift 函数以映射数组?

ios - 动态方法解析

objective-c - Cocos2D拖放 Sprite 问题

ios - 在框架内使用 xcframework

ios - #警告: C-style for statement is deprecated and will be removed in a future version of Swift