您的位置:首页 > 理论基础 > 计算机网络

从网络获取图片的程序

2014-08-04 22:37 190 查看
程序要求:

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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  对象 ios 缓存 url