iOS 图片浏览器程序总结
2015-06-11 18:34
411 查看
转自:http://blog.csdn.net/ttf1993/article/details/44277977
要想浏览几张图片。
1》把每张图片存入每个dictionary,每个字典存一张照片的属性与名字。然后把照片字典存入NSArray。
2》初始化数组,然后得到数组,此时数组已经存满了字典。
3》拿到每个数组的里面的字典。然后显示图片到UIImageView上面。
一般加载的做法:
1》建立一个plist属性文件,然后把每个图片的属性写入进去。
2》因为软件是装在手机上的,读取图片是需要找到绝对路径,所以使用NSBuddle类。功能是得到文件的真是路径。
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"imagelist" ofType:@"plist”];
数组的建立方法就成了:
_imageData = [NSArray arrayWithContentsOfFile:path];
关于得到数组里面的字典的使用:
NSDictionary *imageDict = self.imageData[self.index];//得到字典
self.image.image = [UIImage imageNamed:imageDict[@"key"]];//使用字典
self.descLabel.text = imageDict[@"desc”]; //使用字典
并不是每次查看图片都需要加载图片。看的时候再加载
所以有了延迟加载
if(_imageData == nil) //延迟加载
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"imagelistofType:@"plist"];
_imageData = [NSArray arrayWithContentsOfFile:path];
}
下面是程序:
[objc] view
plaincopyprint?
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self changeImage];
}
- (NSArray *)imageData
{
NSLog(@"调用我了!");
if(_imageData == nil)
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"imagelist" ofType:@"plist"];
_imageData = [NSArray arrayWithContentsOfFile:path];
}
return _imageData;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) changeImage
{
self.headLabel.text = [NSString stringWithFormat:@"%d/%d", self.index+1, self.imageData.count];
NSDictionary *imageDict = self.imageData[self.index];
self.image.image = [UIImage imageNamed:imageDict[@"key"]];
self.descLabel.text = imageDict[@"desc"];
self.lefuBtn.enabled = (self.index != 0);
self.rightBtn.enabled = (self.index != self.imageData.count-1);
}
- (IBAction)left {
self.index--;
[self changeImage];
}
- (IBAction)right {
self.index++;
[self changeImage];
}
要想浏览几张图片。
1》把每张图片存入每个dictionary,每个字典存一张照片的属性与名字。然后把照片字典存入NSArray。
2》初始化数组,然后得到数组,此时数组已经存满了字典。
3》拿到每个数组的里面的字典。然后显示图片到UIImageView上面。
一般加载的做法:
1》建立一个plist属性文件,然后把每个图片的属性写入进去。
2》因为软件是装在手机上的,读取图片是需要找到绝对路径,所以使用NSBuddle类。功能是得到文件的真是路径。
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"imagelist" ofType:@"plist”];
数组的建立方法就成了:
_imageData = [NSArray arrayWithContentsOfFile:path];
关于得到数组里面的字典的使用:
NSDictionary *imageDict = self.imageData[self.index];//得到字典
self.image.image = [UIImage imageNamed:imageDict[@"key"]];//使用字典
self.descLabel.text = imageDict[@"desc”]; //使用字典
并不是每次查看图片都需要加载图片。看的时候再加载
所以有了延迟加载
if(_imageData == nil) //延迟加载
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"imagelistofType:@"plist"];
_imageData = [NSArray arrayWithContentsOfFile:path];
}
下面是程序:
[objc] view
plaincopyprint?
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self changeImage];
}
- (NSArray *)imageData
{
NSLog(@"调用我了!");
if(_imageData == nil)
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"imagelist" ofType:@"plist"];
_imageData = [NSArray arrayWithContentsOfFile:path];
}
return _imageData;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) changeImage
{
self.headLabel.text = [NSString stringWithFormat:@"%d/%d", self.index+1, self.imageData.count];
NSDictionary *imageDict = self.imageData[self.index];
self.image.image = [UIImage imageNamed:imageDict[@"key"]];
self.descLabel.text = imageDict[@"desc"];
self.lefuBtn.enabled = (self.index != 0);
self.rightBtn.enabled = (self.index != self.imageData.count-1);
}
- (IBAction)left {
self.index--;
[self changeImage];
}
- (IBAction)right {
self.index++;
[self changeImage];
}
相关文章推荐
- iOS- 再谈ARC里内存问题,ARC里数组、对象内存得不到释放?
- IBokan_ios80_自学系列_1(switch语句+if...else...语句练习)
- 正则表达式在IOS开发中的应用
- ios毛玻璃效果
- ios多语言设置,操作
- iOS 系统相机的调用
- IOS-视频播放
- ios中Raw文件系统常用文件夹
- iOS内存管理
- iOS8 设置tableView的分割线
- Masonry介绍与使用实践 - iOS移动开发周报(转)
- 使用svn删除Xcode工程中的文件
- iOS NSArray API集合
- 【iOS】苹果,百度Map定位使用与总结
- iOS 使用百度地图地图画成功了,但是没有图层显示问题解决。
- iOS越狱环境开发
- iOS开发之CGPoint、CGSize、CGRect、CGRectMake、window(窗口)、视图(view)
- iOS 枚举类型
- 【iOS】网页中调用JS与JS注入
- 关于iOS Tabbar的一些设置