Asynchronously set images in tableview

前端 未结 4 1153
鱼传尺愫
鱼传尺愫 2020-12-22 12:27

I have a TableView using custom cells. I initially was setting grabbing an image from a URL in the cellForRowAtIndexPath method

- (         


        
4条回答
  •  遥遥无期
    2020-12-22 12:57

    Use this code inside your tableviews cellforindexpath

     NSURLRequest *req =[[NSURLRequest alloc]initWithURL:[NSURL URLWithString:@"yourimageurl.com"]];
    
    [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        if(!error){
            UIImage *image =[UIImage imageWithData:data];
            cell.thumbnailImageView.image = image;
        }
         else{
                //error
       }
    
    
    }];
    

提交回复
热议问题