简体   繁体   English

UICollectionViewCell中图像的异步加载导致标签消失

[英]Asynchronous loading of image in UICollectionViewCell causes labels to disappear

I'm working on a photo gallery app that consists essentially of a navigation controller on to which is loaded instances of a collection view. 我正在开发一个照相馆应用程序,该应用程序基本上由一个导航控制器组成,在该控制器上加载了集合视图的实例。 Each of the collection view cells contains a UIImageView, and a label for the name of the image. 每个集合视图单元格都包含一个UIImageView和一个用于图像名称的标签。 The cells load the images in asynchronously. 单元异步加载图像。

This all works fine the first time each of the collection views are loaded, but if you step back to a previous collection view and forward again the labels disappear. 第一次加载每个收藏夹视图时,这一切都很好,但是如果您退回到上一个收藏夹视图并再次转发,则标签会消失。

I think it must have something to do with loading the images asynchronously, as if I remove the image load the labels are fine, have the correct text and don't disappear. 我认为这与异步加载图像有关,就像我删除图像加载一样,标签很好,文本正确并且不会消失。

I'm not sure where I'm going wrong with the image loading... 我不确定图像加载哪里出问题了...

The various components are: 各个组件是:

Images are loaded using this extension method: 使用以下扩展方法加载图像:

extension UIImageView {
public func imageFromServerURL(url: URL) {

    if ImageData.realPhotoCache.keys.contains(url.absoluteString){
        self.image = ImageData.realPhotoCache[url.absoluteString];
    }else{


        URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) -> Void in

            if error != nil {
                print(error)
                return
            }
            DispatchQueue.main.async(execute: { () -> Void in
                let image = UIImage(data: data!)
                ImageData.realPhotoCache[url.absoluteString] = image;
                self.image = image
            })

        }).resume()
    }
}}

And the cells for the collection view are contucted like this: 集合视图的单元格如下所示:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    var cell: UICollectionViewCell;

    cell = collectionView.dequeueReusableCell(withReuseIdentifier: photoCellReuseIdentifier, for: indexPath);

    let imageProvider = self.imageProviderForIndexPath(indexPath: indexPath);
    (cell as! tsPhotoCollectionViewCell).photoView.imageFromServerURL(url: imageProvider.GetThumbImageUrl());
    (cell as! tsPhotoCollectionViewCell).titleLabel.text = imageProvider.GetImageTitle()!;

    return cell
}

with the collection view being reset when it is returned to like so: 返回时将集合视图重置为:

 func reset(){
    photoProviders = [ImageDataProvider]();
    self.collectionView?.reloadSections(IndexSet(integer: 0))
    startLoad()
}


override func viewWillAppear(_ animated: Bool) {
    if(hasNavigatedAway){
        hasNavigatedAway = false;
        self.reset();
    }
}

The first load of a collection will generally be fine (cells in green, labels in red): 收集的第一个负载通常很好(单元格为绿色,标签为红色):

https://imgur.com/g3p03yF https://imgur.com/g3p03yF

but on naving away and coming back to a collection view, the labels are gone: 但是,随着时间的流逝并返回到集合视图,标签消失了:

https://imgur.com/If2FQZS https://imgur.com/If2FQZS

The labels also seem to come and go with scrolling sometimes. 标签有时也会随滚动而变化。 I've tried everything I can think of, but haven't had any luck.. 我已经尝试了所有我能想到的,但是没有任何运气。

Judging by your screenshots, it seems like UILabels are getting their heights compressed to 0 by expanded UIImageViews. 从您的屏幕截图来看,似乎UILabel通过扩展的UIImageViews将其高度压缩为0。 Try increasing UILabel's vertical content compression resistance priority, say, to 1000, so that it becomes non-compressible (and, in turn, makes auto layout engine compress either UIImageView or spacings - depends on your constraints). 尝试将UILabel的垂直内容压缩优先级提高到1000,以使其不可压缩(然后使自动布局引擎压缩UIImageView或间距-取决于您的约束)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM