从网络获取图片的程序

13 篇文章 0 订阅
13 篇文章 0 订阅

程序要求:

1、把一个含有至少10张图片的地址列表用一个UITableView显示.(required)


2、 然后点击其中一项后用今天的方法下载图片图片并保存到文件中,并跳转或弹出到另一个页面,在页面内显示该图片.(required)

3 、当再次点击的时候,如果该图片已经下载过,就不必再重新下载. (optional)


程序代码:

#import "ViewController.h"
#import "prsentViewController.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    NSArray *imageUrlArr;
    prsentViewController *vctrl;
    UITableView *imageTableView ;
    UITableViewCell *cell;
}
@end

@implementation ViewController
            
- (void)viewDidLoad {
    [super viewDidLoad];
    imageUrlArr = [NSArray arrayWithObjects:
                            @"http://img.nr99.com/attachment/forum/threadcover/7b/c7/123409.jpg",@"http://img.nr99.com/attachment/forum/threadcover/59/bc/123407.jpg",@"http://img.nr99.com/attachment/forum/threadcover/fd/42/123373.jpg",@"http://img.nr99.com/attachment/forum/threadcover/38/1a/123297.jpg",@"http://img.nr99.com/attachment/forum/threadcover/55/2e/123232.jpg",@"http://img.nr99.com/attachment/forum/threadcover/07/39/123342.jpg",@"http://img.nr99.com/attachment/forum/threadcover/25/15/123328.jpg",@"http://img.nr99.com/attachment/forum/threadcover/3f/1d/123231.jpg",@"http://img.nr99.com/attachment/forum/threadcover/14/fd/123306.jpg",@"http://img.nr99.com/attachment/forum/threadcover/df/a8/123348.jpg",nil];
    imageTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    imageTableView.dataSource =self;
    imageTableView.delegate = self;
    [self.view addSubview:imageTableView];
    NSLog(@"%d",imageUrlArr.count);
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return imageUrlArr.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = imageUrlArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSURL *url = [NSURL URLWithString:imageUrlArr[indexPath.row]];
    //所构建的NSURLRequest具有一个依赖于缓存响应的特定策略,cachePolicy取得策略,timeoutInterval取得超时值
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0];
    
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *path = [NSString stringWithFormat:@"%@%@%u.jpg",NSHomeDirectory(),@"/Documents/",indexPath.row];
    [data writeToFile:path atomically:YES];
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];
    vctrl = [[prsentViewController alloc] init];
    vctrl.image = image;
    [self presentViewController:vctrl animated:YES completion:nil];
    
}


#import <UIKit/UIKit.h>

@interface prsentViewController : UIViewController
@property (nonatomic , strong) UIImage *image;

@end

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe)];
    [self.view addGestureRecognizer:swipe];
    ctrl = [[ViewController alloc] init];

    UIImageView *imageView  = [[UIImageView alloc] initWithFrame:CGRectMake(50, 100, 214, 160)];
    imageView.image = self.image;
    [self.view addSubview:imageView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)swipe
{
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    [self dismissViewControllerAnimated:YES completion:nil];
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值