来自 url 的 UIImage 需要很长时间才能快速加载 [英] UIImage from url takes long time to load swift

查看:29
本文介绍了来自 url 的 UIImage 需要很长时间才能快速加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有很多 UITableViewCellUITableView,我使用此代码将内容和图像加载到 UITableCell 但需要很长时间为每个 UITableViewCell 显示图像的时间.

I have a UITableView with A lot of UITableViewCell, I use this code to load content and images to the UITableCell but it takes long time for the image to show for each UITableViewCell.

var loadOnce : [String] = []

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{

    let cell = tableView.dequeueReusableCellWithIdentifier("ProfileActivities", forIndexPath: indexPath) as! ProfileActivitiesTableViewCell


    if !loadOnce.contains("\(indexPath.row)") {

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
            let LocationImage = self.postmap[indexPath.row]
            let LocationImageUrl = NSURL(string: LocationImage)
            let LocationImageData = NSData(contentsOfURL: LocationImageUrl!)

            let ProfileImage = self.postimg[indexPath.row]
            let ProfileImageUrl = NSURL(string: ProfileImage)
            let ProfileImageData = NSData(contentsOfURL: ProfileImageUrl!)

            dispatch_async(dispatch_get_main_queue(), {
                cell.locationImg.image = UIImage(data: LocationImageData!)
                cell.activitesProfileImg.image = UIImage(data: ProfileImageData!)
            });
        });
        loadOnce.append("\(indexPath.row)")

    }
    return cell
}

将代码放入 dispatch_async 会使滚动变得更闷,但加载图像仍然需要时间.

Putting the code in dispatch_async made scrolling smother but its still takes time to load images.

考虑到我有很多图片,比如 Facebook 或 Instagram,如何让它更快地加载图片.

how to make it load images faster considering I have to many images something like Facebook or Instagram.

谢谢.

推荐答案

由于异步调用和 ReUsability 会出现问题.假设您在 IndexPath 1 处的图像正在加载图像,但下载仍然需要时间,同时用户在另一个索引处向下滚动,即 15 并且在内部将索引 1 的相同单元格分配给索引 15,并且将显示单元格 1 的图像在 Cell 15 中,如果您还没有设法处理先前对同一单元格实例的调用.这就是为什么最好使用一些缓存库,如 SDWebImage(正如@SeanLintern88 所说)或 AFNetworking 的 UIImageView 类别

There will be issue because of Async call and ReUsability. Lets say your Image at IndexPath 1 is loading image but it is still taking time to download and meanwhile user scrolled down at some another index i.e. 15 and Internally the same cell of Index 1 is alloted to Index 15 and Image of cell 1 will be displayed in Cell 15 if you have not managed to handle previous calls for same instance of cell. Thats why its Better to use some caching library like SDWebImage(As @SeanLintern88 said) or AFNetworking's UIImageView Category

AlamoFire 的 UIImageView(适用于 Swift)

事件如果你想这样做,那么最好使用子类化,它会帮助你管理调用及其终止.

Event if you wanted to do this, then better is to go with subclassing, it will help you to manage the Calls and Its termination.

在您的情况下,加载图像可能是因为您从服务器加载的实际图像的大小.CDN 类服务提供了一些易于实现的技术,可根据请求提供不同大小的图像.因此,即使用户上传了 1200x1200 大小的图像,但在列表屏幕上您可以获取其 120x120 大小的图像,这有助于加快加载速度并节省物理记忆.

In your case the Loading of Image might be because of Size of actual image that you are loading from server. CDN kind of service provide some easy to implement techniques to serve different size of images as per the request. So even If User has uploaded 1200x1200 sized image but on list screen you can fetch its 120x120 sized image, This helps is faster loading as well as it will save Physical Memories.

这篇关于来自 url 的 UIImage 需要很长时间才能快速加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆