Loading takes a while when i set UIImage to a NSData with a url.

前端 未结 6 1611
独厮守ぢ
独厮守ぢ 2020-12-21 19:33
NSData *imageUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[self.content objectAtIndex:indexPath.row] valueForKey:@\"imageUrl\"] ]];
cell.thumbnailImageV         


        
6条回答
  •  爱一瞬间的悲伤
    2020-12-21 20:12

    Use an NSOperationQueue so that you can perform the loading of the data on a background thread. After that you should set the image on the main queue.

    // Assume queue is created
    [queue addOperationWithBlock:^{
        NSData *imageUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[self.content objectAtIndex:indexPath.row] valueForKey:@"imageUrl"] ]];
    
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            cell.thumbnailImageView.image=[UIImage imageWithData:imageUrl];
        }];
    }];
    

    For more about NSOperationQueue see the docs about NSOperationQueue.

提交回复
热议问题