我最初尝试使用核心图形将图像绘制到单元格中,但是效果很慢,因为我的图像很大。现在我在我的单元格中使用 UIImageViews 在每个单元格中显示 1 张图像。
因为我正在缓存图像,所以在第一次加载每个单元格后它会非常流畅地滚动。不过,第一次加载时,会有一点延迟。
我可以向您展示的内容并不多,因为这是一个相当笼统的问题,但我想我可以向您展示到目前为止我是如何加载图像的:
- (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];
}
}
旁注:我不是指我的调度队列在其他任何地方。它说每个人都需要一个独特的人,所以我破坏了这个随机字符串生成器代码。它是否需要是唯一的,或者它们是否不会再次被提及并不重要?