一、設置代理 NSURLConnectionDatadelegate
web
二、設置動態存儲數據變量 app
@property(nonatomic,strong) NSMutableData * imageData; -(void)viewDidLoad(){ self.imageData=[NSMutableData new]; }
三、設置代理方法dom
#pragma mark 接收數據中 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ [self.imageData appendData:data];//追加數據 } #pragma mark 接收完數據 -(void)connectionDidFinishLoading:(NSURLConnection *)connection{ NSData * data=self.imageData; UIImage * image=[UIImage imageWithData:data]; //第一行很是重要 NSIndexPath * indexPath=[NSIndexPath indexPathForRow:3 inSection:0]; UITableViewCell * cell= [self.tableView cellForRowAtIndexPath:indexPath]; UIImageView * imageView=[cell viewWithTag:1]; [imageView setImage:image]; [self.imageData setLength:0]; } -(void)loadFace{ NSURL * url=[NSURL URLWithString:@"http://www.d-shang.com/static/attached/face/201511/s_20151106085677nh.jpg"]; NSURLRequest * request=[[NSURLRequest alloc]initWithURL:url]; NSURLConnection * connect=[[NSURLConnection alloc]initWithRequest:request delegate:self]; [connect start]; }
//結果仍是無法實現ide
用如下方法能夠atom
#import <SDWebImage/UIImageView+WebCache.h>... - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *MyIdentifier = @"MyIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease]; } // Here we use the new provided sd_setImageWithURL: method to load the web image [cell.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]]; cell.textLabel.text = @"My Text"; return cell; }