网络编程图片下载

网络编程图片下载
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;
}

转载于:https://my.oschina.net/jlong/blog/480996

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值