viewDidLoad后加载数据
我有一个 viewController,它要求我加载一系列图像并将它们显示在屏幕上,但是在 viewDidLoad 中加载这些图像需要一段时间,并且在视图出现在屏幕上之前存在延迟。
有没有一种方法可以让我在 viewDidLoad 中只加载屏幕上可见的前 5 个图像,然后在 viewController 加载后立即加载剩余的图像?
这是 viewWillAppear 或 viewDidAppear 的用途吗? 是否有其他地方最适合用于加载内容而不延迟用户的响应时间?
非常感谢!
I have a viewController that requires I load a whole series of images and display them on the screen, but loading those images in viewDidLoad takes a while and there is a delay before the view appears on screen.
Is there a way that I could load just the first 5 images that are visible on the screen in viewDidLoad, and then load the remaining images immediately after the viewController has loaded?
Is this what viewWillAppear or viewDidAppear are for?
Is there somewhere else that is best used for loading content without delaying the response time for the users?
Thanks so much in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
viewWillAppear
来显示前 5 个图像/数据,而不会对用户造成任何延迟,并在viewDidAppear
中显示剩余数据。you could use
viewWillAppear
to get first 5 images/data to display without any delay to user and rest data you could inviewDidAppear
.如果您只想在加载图像之前显示视图,您当然可以在
viewDidAppear
中执行此操作。请记住,viewDidAppear
可能会被多次调用,并且您可能只想加载图像。如果您从互联网下载图像(即加载时间较长),您可能需要实现一个图像加载器,让您的控制器知道图像已准备好通过委托显示。如果所有图像都存储在本地并在几秒钟内加载,则 viewDidAppear 方法应该可以正常工作。
You could certainly do it in
viewDidAppear
, if you just want to show the view before loading the images. Keep in mind thatviewDidAppear
may be called more than once and you probably only want to load the images ones.If you're downloading the images from the internet (i.e. long loading times), you might want to implement an image loader that will let you're controller know that the image is ready to be displayed through a delegate. If all the images are stored locally and load within seconds the
viewDidAppear
approach should work fine.我建议您使用 GCD 或 NSInspiration 在后台线程上加载图像。在 viewDidLoad 中,您只需启动后台处理。将图像加载到后台线程后,将它们传递到主线程并按您的意愿显示。
I would recommend you loading images on a background thread(s) either using GCD or NSInvocation. In viewDidLoad you'd just launch background processing. after images are loaded on a background thread, pass them to a main thread and display as you wish.