您的位置:首页 > 移动开发 > IOS开发

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];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: