在加载完整图像之前使用占位符图像 [英] Using a placeholder image before loading the full image

查看:74
本文介绍了在加载完整图像之前使用占位符图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用占位符图像的正确方法是什么?

What is the correct way to use a placeholder image?

我想用图像作为子视图填充UIScrollView,但是在显示滚动视图之前,要在较旧的设备上加载所有完整的图像就很麻烦了.

I want to fill a UIScrollView with images as subviews, but it has been to heavy on the older devices to load in all the full images before showing the scrollview.

如何使用占位符图像,以便当用户滚动到该contentOffset时可以加载完整图像?

How can a placeholder image be used so that I can load in the full image when the user scrolls to that contentOffset?

我尝试使用ALAsset的缩略图,但是缩放比例看起来很糟糕.

I tried using the thumbnail of the ALAsset, but the scaling looks horrible.

推荐答案

假设您正在使用UIImageView子视图作为图像图块.一开始,您可以像这样填充UIImageView子视图:

Suppose you're using UIImageView subviews for your image tiles. At the beginning, you can populate your UIImageView subviews like so:

 for each tile location in myScrollView area:
    [myScrollView addSubview:[UIImageView 
       initWithImage:[UIImage imageNamed:@"placeholder.png"]]];

这里的关键是imageNamed确实在后台进行缓存(请参阅SDK文档),并且内存中仅存在一个实际的placeholder.png图像.

The key here is that imageNamed does caching behind the scenes (see the SDK docs), and only one actual placeholder.png image will exist in memory.

在UIScrollView事件的委托中,当用户在未加载其内容的新图块上滚动并沉降时,启动加载过程并在该位置处替换UIImageView(删除占位符,添加适当的占位符).

In your delegate to UIScrollView events, when user has scrolled and settled on a new tile that doesn't have its content loaded, set off the loading process and replace the UIImageView at that location (remove the placeholder, add the proper one).

显然,您可能还希望在某些时候卸载旧的图像图块以节省内存.

Obviously, you might want to unload old image tiles too at some point to save memory.

这篇关于在加载完整图像之前使用占位符图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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