这是我的问题:
我有一个UITableView
包含 customUITableViewCell
的。其中每一个UITableViewCell
(称为HomePicCell
)都与一个Pic
对象相关联,该对象具有指向图像 URL 的属性。一旦我的单元格显示出来,我就开始使用SDWebImage manager
.
一切运行顺利,但20 到 80 秒后,一些线程开始占用 CPU。然后该设备成为那些寒冷冬夜的完美手加热器,但我现在宁愿跳过这个功能!
我真的无法确定会导致此问题的原因。我不认为保留循环会成为问题,因为它只会占用内存。一个经过实验的意见真的会有所帮助。
这是我的代码:
UITableView 数据源
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* cellIdentifier = [@"HomePicCell" stringByAppendingString:[Theme sharedTheme].currentTheme];
HomePicCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[HomePicCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
if(self.pics.count>0){
Pic* pic = self.pics[indexPath.section];
[cell configureWithPic:pic];
}
return cell;
}
HomePicCell (configureWithPic:)
- (void)configureWithPic:(Pic*)pic
{
self.pic = pic;
// Reinit UI
[self.progressView setHidden:NO];
[self.errorLabel setHidden:YES];
[self.photoImageView setAlpha:0];
[self.progressView setProgress:0];
[self.pic downloadWithDelegate:self];
}
图片
- (void) downloadWithDelegate:(id<PicDownloadDelegate>)delegate
{
SDWebImageManager *manager = [SDWebImageManager sharedManager];
[manager downloadWithURL:self.url options:0 progress:^(NSUInteger receivedSize, long long expectedSize) {
if(expectedSize>0){
float progress = [@(receivedSize) floatValue]/[@(expectedSize) floatValue];
[delegate pic:self DownloadDidProgress:progress];
}
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {
self.isGif = @(image.images.count>1);
if(image){
if(cacheType == SDImageCacheTypeNone){
[delegate pic:self DownloadDidFinish:image fromCache:NO];
}else{
[delegate pic:self DownloadDidFinish:image fromCache:YES];
}
}else{
[delegate pic:self DownloadFailWithError:error];
}
}];
}
HomePicCell(委托方法)
- (void)pic:(Pic*)pic DownloadDidFinish:(UIImage *)image fromCache:(BOOL)fromCache
{
if(![pic isEqual:self.pic]){
return;
}
[self.progressView setHidden:YES];
self.photoImageView.image = image;
[self updateUI];
}
- (void)pic:(Pic*)pic DownloadFailWithError:(NSError *)error
{
if(![pic isEqual:self.pic]){
return;
}
[self.errorLabel setHidden:NO];
[self.progressView setHidden:YES];
}
- (void)pic:(Pic*)pic DownloadDidProgress:(float)progress
{
if(![pic isEqual:self.pic]){
return;
}
[self.progressView setProgress:progress+.01f];
}
谢谢 !