网络编程图片下载
1-先获取图片,在NSData中有很多datawith……的方法,根据情况 选择适合的方法
UIImageView *img = [[UIImageViewalloc]initWithFrame:CGRectMake(0, 0, 375, 667)];
这一步设置图片展示位置
UIImage *picture=[[UIImagealloc]init];
添加图片对象
NSString *pathOfFileString=@"http://d.hiphotos.baidu.com/image/w%3D310/sign=7921914313dfa9ecfd2e501652d1f754/6159252dd42a2834bf69c55459b5c9ea14cebfa6.jpg";
NSData *data=[NSDatadataWithContentsOfURL:[NSURLURLWithString:pathOfFileString]];
获取网页上的图片为数据流
picture=[UIImageimageWithData:data];
将数据转化为图片
img.image=picture;
[self.viewaddSubview: img];
将图片加在 UIImageView上展示。
2-将获取的图片保存
UIImage *picture=[[UIImagealloc]init];
NSString *pathOfFileString=@"http://d.hiphotos.baidu.com/image/w%3D310/sign=7921914313dfa9ecfd2e501652d1f754/6159252dd42a2834bf69c55459b5c9ea14cebfa6.jpg";
NSData *data=[NSDatadataWithContentsOfURL:[NSURLURLWithString:pathOfFileString]];
picture=[UIImageimageWithData:data];
NSString *pathStr=@"/Users/feifanchengxuyuan/Desktop/tupianTest_1.jpg";
[UIImageJPEGRepresentation(picture, 1.0) writeToFile:pathStr options:NSAtomicWriteerror:nil];
[pathStr stringByAppendingPathComponent:pathOfFileString];
注意:
获取图片并保存,注意获取图片后要先将数据存为图片在按不同格式选择方法保存。
可以整体封装为方法如下:
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(@"保存路径:%@",documentsDirectoryPath);
//获取图片
UIImage * imageFromURL = [selfgetImageFromURL:@"http://d.hiphotos.baidu.com/image/w%3D310/sign=7921914313dfa9ecfd2e501652d1f754/6159252dd42a2834bf69c55459b5c9ea14cebfa6.jpg”];
//保存图片
[selfsaveImage:imageFromURL withFileName:@"MyImage"ofType:@"jpg"inDirectory:@"/Users/mybirdie/Desktop”];
//加载图片
UIImage * imageFromWeb = [selfloadImage:@"MyImage"ofType:@"jpg"inDirectory:@"/Users/mybirdie/Desktop”];
UIImageView *img = [[UIImageViewalloc]initWithFrame:CGRectMake(0, 0, 375, 667)];;
[img setImage:imageFromWeb];
[self.viewaddSubview: img];
}
- (void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
if ([[extension lowercaseString] isEqualToString:@"png"]) {
[UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSStringstringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWriteerror:nil];
} elseif ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
[UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSStringstringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWriteerror:nil];
} else {
NSLog(@"文件后缀不认识");
}
}
-(UIImage *) loadImage:(NSString *)fileName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
UIImage * result = [UIImageimageWithContentsOfFile:[NSStringstringWithFormat:@"%@/%@.%@", directoryPath, fileName, extension]];
return result;
}
-(UIImage *) getImageFromURL:(NSString *)fileURL {
NSLog(@"执行图片下载函数");
UIImage * result;
NSData * data = [NSDatadataWithContentsOfURL:[NSURLURLWithString:fileURL]];
result = [UIImageimageWithData:data];
return result;
}