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

前端 未结 6 1623
独厮守ぢ
独厮守ぢ 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:17

    I would suggest you to use AsyncImageView a beautiful implementation by Nicklockwood -father of iCarousel.

    Link it is very simple to use.

        #import "AsyncImageView.h"
    

    and in all imageViews do this.

        [imageView setImage:@"default.png"];
        [imageView setImageURL:@"whateverurl/whateverimage.png"];
    

    In your case it would be:

        [cell.thumbnailImageView setImageURL:@"yourURL"];
    

    It works like a charm, and my code is in production. But if you still want your code to work try this:

     UIActivityIndicator *activity =[[UIActivityIndicator alloc] initWithStyle:UIActivityIndicatorWhite];
     [activity setFrame:CGRectMake(0,0,30,30)];
     [cell.contentView addSubview:activity];
    
     cell.thumbnailImageView.image=[UIImage imageNamed:@"Default~cell~image.png"];      
    
     dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
     dispatch_async(dispatchQueue, ^(void)
     { 
       [activity startAnimating];
       [self loadImages];
       dispatch_sync(dispatch_get_main_queue(), ^{ 
               NSData *imageUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[self.content objectAtIndex:indexPath.row] valueForKey:@"imageUrl"] ]];
               cell.thumbnailImageView.image=[UIImage imageWithData:imageUrl];          [activity stopAnimating];
               [activty setHidden:YES];
        });
     }); 
    

提交回复
热议问题