2012-05-29 72 views
0

我最初嘗試使用核心圖形繪製我的圖像到單元格中,但這是工作緩慢,因爲我的圖像是相當大的。現在我在我的單元格中使用UIImageViews在每個單元格中顯示1個圖像。我如何加快UITableViewCell中的UIImageView/UIButton?

因爲我正在緩存圖像,所以在第一次加載每個單元格後,它會非常順利地進行滾動。第一次加載時,會有一點滯後。

有沒有真的很多,我可以告訴你,因爲這是一個非常通用的問題,但我想我可以告訴你我是如何加載我的圖片至今:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 

     self.isReadyForImage = NO; 

     NSString *generated = [Entry generateString]; 

     const char *cString = (const char*)[generated UTF8String]; 

     dispatch_queue_t queue = dispatch_queue_create(cString, NULL); 
     dispatch_async(queue, ^{ 

      self.thumbnailButton = [[UIButton alloc] init]; 
      self.thumbnailButton.contentMode = UIViewContentModeCenter; 
      self.thumbnailButton.layer.shadowColor = [[UIColor blackColor] CGColor]; 
      self.thumbnailButton.layer.shadowOffset = CGSizeMake(0, 2); 
      self.thumbnailButton.layer.shadowOpacity = 0.2; 
      self.thumbnailButton.layer.shadowRadius = 3; 
      self.thumbnailButton.userInteractionEnabled = YES; 

      dispatch_async(dispatch_get_main_queue(), ^{ 
       self.isReadyForImage = YES; 

       if (self.imageToBecomeThumbnail != nil) { 
       [self setupThumbnail]; 
       } 
      }); 

     }); 
self.selectionStyle = UITableViewCellSelectionStyleNone; 

    } 
    return self; 
} 
-(void)resetShadowPath { 
    self.thumbnailButton.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.thumbnailButton.bounds].CGPath; 
} 

-(void)setThumbnail:(UIImage *)image { 

    self.imageToBecomeThumbnail = image; 

    if (self.isReadyForImage == YES) { 
     [self setupThumbnail]; 
    } 
} 

-(void)setupThumbnail { 
    [self.thumbnailButton setImage:self.imageToBecomeThumbnail forState:UIControlStateNormal]; 

    self.thumbnailButton.frame = CGRectMake(0, IMAGE_SPACING, self.imageToBecomeThumbnail.size.width, self.imageToBecomeThumbnail.size.height); 
    self.thumbnailButton.center = CGPointMake(self.bounds.size.width/2, self.thumbnailButton.center.y); 
    [self resetShadowPath]; 
    [self.thumbnailButton addTarget:self action:@selector(thumbnailPressed:) forControlEvents:UIControlEventTouchUpInside]; 

    if (self.thumbnailButton.superview == nil) { 
     [self addSubview:self.thumbnailButton]; 
    } 
} 

附註:我不是指我的調度隊列的其他地方。它說每一個都需要一個獨特的,所以我把這個隨機字符串生成器代碼分解出來。它是否必須是獨一無二的,還是不重要,如果他們不會再被提及?

回答

0

獨立於滾動填充緩存。只要應用程序啓動或新數據可用,就開始在第二個線程中生成缺失的縮略圖。