【问题标题】:I'm new to iOS Development, and I want to learn lazy loading of images in tableview? [duplicate]我是 iOS 开发的新手,我想学习在 tableview 中延迟加载图像? [复制]
【发布时间】:2026-01-13 11:45:01
【问题描述】:

可能重复:
Lazy load images in UITableView

我有 40 行的 tableview,我想从 URL 向每个单元格添加图像。 但问题是,当我启动我的应用程序时,它会冻结一段时间,直到从各种 URL 加载所有图像? 我希望图像在加载时一张一张地显示,并且我的应用程序不能冻结。 所以请回答我...?

【问题讨论】:

    标签: ios lazy-loading


    【解决方案1】:
    @interface ImageLoading : UIView {
    
    NSURLConnection* connection;
    NSMutableData* data;
    NSArray *userArr1;
    UIActivityIndicatorView *progress;
    }
    
     @property (nonatomic, retain) NSArray *userArr1;
    
      - (void)loadImageFromURL:(NSURL*)url;
    
      @end
    

    然后在实现文件中

      @implementation ImageLoading
    
       - (id)initWithFrame:(CGRect)frame
      {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        }
     return self;
       }
    
       - (void)loadImageFromURL:(NSURL*)url {    
    
       progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(5, 5, 25, 25)];
    progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    [self addSubview:progress];
    [progress startAnimating];
    
    NSURLRequest* request =[NSURLRequest requestWithURL:url];
    
    connection = [[NSURLConnection alloc]
                  initWithRequest:request delegate:self];
       }
    
           - (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {
    
    if (data==nil) {
        data =
        [[NSMutableData alloc] initWithCapacity:2048];
    }
    
    [data appendData:incrementalData];
     }
    
      - (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
    
           connection=nil;
    
           if ([[self subviews] count]>0) {
        [[[self subviews] objectAtIndex:0] removeFromSuperview];
           }
    
          UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage  imageWithData:data]];
         imageView.frame = self.bounds;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight );    
    
    // Begin a new image that will be the new image with the rounded corners 
    // (here with the size of an UIImageView)
    UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
    
    // Add a clip before drawing anything, in the shape of an rounded rect
    [[UIBezierPath bezierPathWithRoundedRect:imageView.bounds 
                                cornerRadius:5.0] addClip];
    // Draw your image
    [[UIImage imageWithData:data] drawInRect:imageView.bounds];
    
    // Get the image, here setting the UIImageView image
    imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    
    // Lets forget about that we were drawing
    UIGraphicsEndImageContext();
    
    [self addSubview:imageView];
    [progress stopAnimating];
    
    [imageView setNeedsLayout];
    [self setNeedsLayout];
    data=nil;
      }
    
      - (UIImage*) image {
    
     UIImageView* iv = [[self subviews] objectAtIndex:0];
     return [iv image];
       }
    
     @end
    

    在您的表格视图单元格中

       ImageLoading* asyncImage = [[ImageLoading alloc]
                                   initWithFrame:frame];
        asyncImage.tag = 999;
        [asyncImage loadImageFromURL:url];
      [cell.contentView addSubview:asyncImage];
    

    你可以使用this

    同样的question

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
    • 现在我用基本代码编辑了
    • 与其只是从另一个答案中复制代码,不如将问题标记为重复并链接到另一个问题。