iOS 图片浏览器程序总结


转自: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 plain copy print ?
  1. @implementation ViewController  
  2.   
  3. - (void)viewDidLoad {  
  4.     [super viewDidLoad];  
  5.     [self changeImage];  
  6. }  
  7.   
  8. - (NSArray *)imageData  
  9. {  
  10.     NSLog(@"调用我了!");  
  11.     if(_imageData == nil)  
  12.     {  
  13.         NSBundle *bundle = [NSBundle mainBundle];  
  14.         NSString *path = [bundle pathForResource:@"imagelist" ofType:@"plist"];  
  15.           
  16.         _imageData = [NSArray arrayWithContentsOfFile:path];  
  17.     }  
  18.       
  19.     return _imageData;  
  20. }  
  21.   
  22. - (void)didReceiveMemoryWarning {  
  23.     [super didReceiveMemoryWarning];  
  24.     // Dispose of any resources that can be recreated.  
  25. }  
  26.   
  27. -(void) changeImage  
  28. {  
  29.     self.headLabel.text = [NSString stringWithFormat:@"%d/%d"self.index+1self.imageData.count];  
  30.     NSDictionary *imageDict = self.imageData[self.index];  
  31.     self.image.image = [UIImage imageNamed:imageDict[@"key"]];  
  32.     self.descLabel.text = imageDict[@"desc"];  
  33.       
  34.     self.lefuBtn.enabled = (self.index != 0);  
  35.     self.rightBtn.enabled = (self.index != self.imageData.count-1);  
  36. }  
  37.   
  38. - (IBAction)left {  
  39.     self.index--;  
  40.     [self changeImage];  
  41. }  
  42.   
  43. - (IBAction)right {  
  44.     self.index++;  
  45.     [self changeImage];  
  46. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值