// 1.创建 GET 请求
// 请求地址
NSString *str = @"图片的网址";
// 对字符串进行编码,将汉字等特殊字符转为 UTF-8格式
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// 把字符串转换成 url 格式
NSURL *url = [NSURL URLWithString:str];
// 网络请求,
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 设置网络请求格式,默认是 get 请求
request.HTTPMethod = @"GET";
// 2.发送请求
// 发送请求有两种方式:同步,异步
// 异步的方式比较好
// **代表的是地址指针
NSURLResponse *response = nil;
NSError *error = nil;
// 同步,异步请求
// 参数1:创建好的请求
// 参数2:服务器的相应信息
// 参数3:错误信息
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// 如果存在错误,打印错误信息
if (error) {
NSLog(@"错误信息:%@",error);
}
if (response) {
NSLog(@"响应信息:%@",response);
}
// 3.处理数据
// 把接收的图片接收
UIImage *image = [UIImage imageWithData:data];
// 把 image 加载到图片
self.imageView.image = image;