IOS - SWIFT - CollectionView - > ImageView Load URL Image



我有一个收集视图,该图像应从net加载。

URL在字符串中,在CollectionView单元格中是ImageView。

我找到了此代码:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    var cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! ViewCell
    var url = NSURL(string: "(array[indexPath.row])")
    var err: NSError?
    var imageData: NSData = NSData(contentsOfURL: url!, options: NSDataReadingOptions.DataReadingMappedIfSafe, error: &err)!
    var bgImage = UIImage(data:imageData)
    cell.imgView.image = bgImage
   // cell.imgView.image = UIImage(named: array[indexPath.row])
    cell.load.hidden = false
    cell.load.startAnimating()
    cell.load.hidesWhenStopped = true
    if cell.imgView.image == nil {
        cell.load.startAnimating()
    }else{
        cell.load.stopAnimating()
    }
    return cell
}

问题就在开始时出现。应用程序已加载,我在我的mainvc中看到了一些来自println的coments(带有CollectionView),但在iPad上它只是frezen启动屏幕。

我试图将切入点调到另一个视图,直到我将其转移到mainvc之前,一切都在任何触摸上都没有恢复。

那怎么了?

感谢您的任何帮助!

这是因为您将图像加载到下面的主线程上。

var imageData: NSData = NSData(contentsOfURL: url!, options: NSDataReadingOptions.DataReadingMappedIfSafe, error: &err)!

下载图像后,异步下载图像并更新相应的单元格。

示例:https://developer.apple.com/library/prerelease/ios/samplecode/lazytableimages/introduction/introduction/intro.html

http://jamesonquave.com/blog/developing-ios-apps-using-swift-part-5-5-ashnc-image-loading-loading-and-caching/

最新更新