// 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;