网络请求图片

来源:互联网 发布:mac电脑重装系统 编辑:程序博客网 时间:2024/06/05 19:08

网络请求图片我们常用的方式有两种

1.用于请求少量的图片,否则会很卡。对于请求的URL里面有汉字的必须要NSUTF8StringEncoding编码,没有的可以不用编译。

    NSURL * roomImageurl=[NSURLURLWithString:[locallabelRoomImageUrl(roomName)stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

//    NSURL * url=[roomImageurl string]

    NSLog(@"%@",locallabelRoomImageUrl(roomName));

    NSData * roomImageData=[NSDatadataWithContentsOfURL:roomImageurl];

    UIImage * image=[UIImageimageWithData:roomImageData];



2.用SDWebImage下载,具体的代码如下

//生成图片链接NSURL *picURL = [NSURL URLWithString:string];//从网络获取照片,如果没有就用系统自带占位照片[UIImageView sd_setImageWithURL:picURL];//下面这个方法多了个参数 placeholderImage,意思是如果没有从网络请求到数据,用使用我们预先设置的这张照片。[UIImageView sd_setImageWithURL:picURL placeholderImage:[UIImage imageNamed:@"picholder.png"]];



0 0