zoukankan      html  css  js  c++  java
  • UITableView image gcd

     1 if (![[NSFileManager defaultManager] fileExistsAtPath:[path stringByAppendingPathComponent:@"image.png"]])
     2 {
     3     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,  0ul);
     4     dispatch_async(queue, ^{
     5         NSString *url=[pat stringByAppendingPathComponent:@"comments.txt"];
     6         NSString *u=[NSString stringWithContentsOfFile:url encoding:NSUTF8StringEncoding error:nil];
     7         NSURL *imageURL=[NSURL URLWithString:u];
     8         NSData *image=[NSData dataWithContentsOfURL:imageURL];
     9         [image writeToFile:[pat stringByAppendingPathComponent:@"image.png"] atomically:YES];
    10         dispatch_sync(dispatch_get_main_queue(), ^{
    11             UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
     cell.imageView.image=[UIImage imageWithContentsOfFile:[pat stringByAppendingPathComponent:@"image.png"]];
    12             cell.imageView.image=[UIImage imageWithContentsOfFile:[pat stringByAppendingPathComponent:@"image.png"]];
    13             [cell setNeedsLayout];
    14             NSLog(@"Download");
    15         });
    16     });
    17     cell.imageView.image=[UIImage imageNamed:@"placeholder"];
    18 }
    19 else
    20 {
    21     NSLog(@"cache");
    22     cell.imageView.image=[UIImage imageWithContentsOfFile:[pat stringByAppendingPathComponent:@"image.png"]];
    23 }

    Rather than capturing the cell you need to capture the index path, then get the cell back using:

    UITableViewCell*cell =[tableView cellForRowAtIndexPath:indexPath];

    That way, if the cell is now off screen you'll get nil back and the image won't be set on the wrong cell.

    The other thing you need to add after your dispatch_async() is a

    cell.imageView.image=somePlaceholderImage.

  • 相关阅读:
    Codeforces 547D. Mike and Fish 题解
    Codeforces 521E. Cycling City 题解
    Codeforces 585E. Present for Vitalik the Philatelist 题解
    Codeforces 605E. Intergalaxy Trips 题解
    AGC033D
    第3次作业:卷积神经网络
    Linux下如何编写go语言程序实现菲波拉契数列
    C语言常见典型错误汇总(助教)
    第一次作业:深度学习基础
    数论复习_欧几里得算法与扩展欧几里得算法
  • 原文地址:https://www.cnblogs.com/cokecoffe/p/2760820.html
Copyright © 2011-2022 走看看