初始化 plist数据
- (NSArray *)pic
{
if (_pic == nil) {
// 写代码加载pic.plist文件中的数据到_pic
// 1. 获取pic.plist文件的路径
// 获取pic.plist文件的路径赋值给path变量
// [NSBundle mainBundle]表示获取这个app安装到手机上时的根目录
// 然后在app的安装的根目录下搜索pic.plist文件的路径
NSString *path = [[NSBundlemainBundle] pathForResource:@"pic.plist"ofType:nil];
// 读取文件
NSArray *array = [NSArrayarrayWithContentsOfFile:path];
_pic = array;
}
return_pic;
}
从列表中读取对应数据展示
// 1. 让索引++
self.index++;
// 2. 从数组中获取当前这张图片的数据
NSDictionary *dict = self.pic[self.index];
// 3. 把获取到的数据设置给界面上的控件
self.lblIndex.text = [NSStringstringWithFormat:@"%d/%ld", self.index + 1, self.pic.count];
// 通过image属性来设置图片框里面的图片
self.imgViewIcon.image = [UIImageimageNamed:dict[@"icon"]];
// 设置这张图片的标题
self.lblTitle.text = dict[@"title”];
图片内存占用释放问题
// 方法一。使用这种方式加载图片, 加载起来的图片即便没有强类型指针引用也不会销毁(会被缓存)
UIImage *imgCat = [UIImage imageNamed:imgName];
// 方法二。使用下面这种方式加载的图片, 只要没有强类型指针引用就会被销毁了
// 解决:换一种加载图片的方式, 不要使用缓存
// 获取图片的完成的路径
NSString *path = [[NSBundlemainBundle] pathForResource:imgName ofType:nil];
// 这里的参数不能再传递图片名称了, 这里需要传递一个图片的完整路径(不被缓存)这种调用,图片需要在supporting file目录下。
UIImage *imgCat = [UIImageimageWithContentsOfFile:path];
// 设置图片框在调用setAnimationImages:nil方法的时候延迟执行(动画结束后释放内存)
[self.imgViewCatperformSelector:@selector(setAnimationImages:) withObject:nilafterDelay:self.imgViewCat.animationImages.count * 0.1];