網絡請求圖片我們常用的方式有兩種
1.用於請求少量的圖片,否則會很卡。對於請求的URL裏面有漢字的必須要NSUTF8StringEncoding編碼,沒有的可以不用編譯。
NSURL * roomImageurl=[NSURL URLWithString:[locallabelRoomImageUrl(roomName) stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
// NSURL * url=[roomImageurl string]
NSLog(@"%@",locallabelRoomImageUrl(roomName));
NSData * roomImageData=[NSData dataWithContentsOfURL:roomImageurl];
UIImage * image=[UIImage imageWithData:roomImageData];
2.用SDWebImage下載,具體的代碼如下
//生成圖片鏈接
NSURL *picURL = [NSURL URLWithString:string];
//從網絡獲取照片,如果沒有就用系統自帶佔位照片
[UIImageView sd_setImageWithURL:picURL];
//下面這個方法多了個參數 placeholderImage,意思是如果沒有從網絡請求到數據,用使用我們預先設置的這張照片。
[UIImageView sd_setImageWithURL:picURL placeholderImage:[UIImage imageNamed:@"picholder.png"]];