从网络获取图片的程序
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
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]; }
相关文章推荐
- Android程序如何实现从网络中获取一张图片
- 微信小程序的条件、遍历、网络请求、获取本地图片
- Android程序如何实现从网络中获取一张图片
- 微信小程序入门二: 条件、遍历、网络请求、获取本地图片
- Java学习笔记之网络编程基础-通过URL获取网络图片
- 获取网络图片缓存的地址
- Android从网络获取图片并放大
- 使用程序保存网页中的图片 网络读取图片
- 获取FCK网络编辑器里的图片路径
- C# 检查网络是否连通 判断远程文件是否存在 C#获取程序路径的方法中需要注意的地方
- android获取互联网图片与源代码——网络编
- 利用swt中的一个获取图片的类SwingResourceManager 来解决将程序打为jar后图片找不着的问题
- V4L2摄像头获取单幅图片测试程序
- V4L2摄像头获取单幅图片测试程序(read/write模式)
- V4L2摄像头获取单幅图片测试程序(MMAP模式)
- 在VB上轻松编写读取图片验证码的网络程序(转)
- 巧妙利用TWebBrowser让程序直接显示网络图片(Delphi)
- 如何根据字符串获取嵌入到程序中的图片等资源
- js 获取网络图片的高度和宽度的实现方法(变通了下)
- Android中获取网络图片的方法(如果手机缓存里面有就从缓存获取)