원래 이미지를 셀에 그릴 때 코어 그래픽을 사용해 보았지만 이미지가 상당히 크기 때문에 느리게 작동했습니다. 이제는 각 셀에 1 개의 이미지를 표시하기 위해 UIImageViews를 셀에 사용하고 있습니다.어떻게 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];
}
}
사이드 참고 : 난 내 디스패치 대기열을 다른 곳으로 참조하지 않습니다. 그것은 각각 하나가 고유 한 것이 필요하다고 말했고, 그래서이 임의의 문자열 생성기 코드를 파기했습니다. 고유해야 할 필요가 있습니까, 아니면 다시 언급하지 않을지는 중요하지 않습니까?