xcode 如何在 iPhone 上的 UITableView 中预加载或延迟加载图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2944924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:24:11  来源:igfitidea点击:

How to preload or lazyload images in a UITableView on iPhone?

iphoneobjective-cxcode

提问by Mr_Nizzle

I'm having a problem when scrolling down and up a tableView on iPhone it gets kind of stuck while loading the images i need a method to preload the images so the scroll can be fluid or load the images 'till the scroll event stops...

我在 iPhone 上向下和向上滚动 tableView 时遇到问题 它在加载图像时有点卡住我需要一种方法来预加载图像以便滚动可以流畅或加载图像直到滚动事件停止.. .

any help on this?

对此有任何帮助吗?

回答by FRotthowe

I found this articlevery helpful. It provides a very detailed example for lazy loading of images in the main runloop.

我发现这篇文章很有帮助。它提供了一个非常详细的示例,用于在主 runloop 中延迟加载图像。

回答by falconcreek

Lazy Table Images Sample Codeincluded in iPhone Developer Resources. Check out Photo Locations Sample Codeas well. It uses Core Data and "lazy loading" techniques if your image files are stored on the device.

iPhone 开发人员资源中包含的懒惰表图像示例代码。也请查看照片位置示例代码。如果您的图像文件存储在设备上,它会使用 Core Data 和“延迟加载”技术。

回答by Pawel

Well, generally speaking, preloading images for table view is a bad idea. Think of a following situation: you have 100 cells, each containing one image. That means you'll have to load 100 images before showing the table, which will have a serious impact on avaliable memory.

嗯,一般来说,为表格视图预加载图像是一个坏主意。考虑以下情况:您有 100 个单元格,每个单元格包含一个图像。这意味着您必须在显示表格之前加载 100 个图像,这将对可用内存产生严重影响。

The "loading effect" you're talking about can be caused by two things: a) as you have said, the problem with loading images. If so, do it in another thread, and pass the image to main thread when it loads b) You're using images that are bigger or smaller than the given size. This will result in resizing the image when it appears on screen, causing the ugly stop-effect - the solution is to draw images in size which is exactly the same as their size.

您所说的“加载效果”可能是由两件事引起的:a)正如您所说,加载图像的问题。如果是这样,请在另一个线程中执行此操作,并在加载时将图像传递给主线程 b) 您使用的图像大于或小于给定尺寸。这将导致图像出现在屏幕上时调整图像大小,从而导致丑陋的停止效果 - 解决方案是绘制与图像大小完全相同的图像。

Chope this was helpful, Paul

Chope 这很有帮助,保罗

回答by Jonathan.

Not sure about images, but with text I have downloaded JSON files when the view loads, parsed into an array and then loaded the tableview with that array.

不确定图像,但对于文本,我在视图加载时下载了 JSON 文件,解析为一个数组,然后使用该数组加载 tableview。