- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell" forIndexPath:indexPath]; cell.tag = indexPath.row; //cell.imageView.image = nil; // Rounded Rect for cell image CALayer *cellImageLayer = cell.imageView.layer; [cellImageLayer setCornerRadius:25]; [cellImageLayer setMasksToBounds:YES]; [self getImages]; [self storeImages]; UIImage *image =_ResimSonHali[indexPath.row]; dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0); dispatch_async(queue,^(void) { if (image) { dispatch_async(dispatch_get_main_queue(),^{ if (cell.tag == indexPath.row) { CGSize itemSize = CGSizeMake(50,50); UIGraphicsBeginImageContext(itemSize); CGRect imageRect = CGRectMake(0.0,0.0,itemSize.width,itemSize.height); [image drawInRect:imageRect]; // cell.ThumbImage.image = image1; cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); [cell setNeedsLayout]; } }); } }); cell.TitleLabel.text = _TarifAdi[indexPath.row]; return cell; } -(void)getImages { NSMutableArray *fuckingArrayYemek = [[NSMutableArray alloc] init]; for (int i=0; i<[_ResimAdiBase count]; i++) { NSString *testString=_ResimAdiBase[i]; NSArray *ImageNames = [testString componentsSeparatedByString:@"."]; [self cacheImage: _ResimAdi[i] : ImageNames[0] ]; [fuckingArrayYemek addObject:ImageNames[0]]; } _ResimSonAdi = fuckingArrayYemek; } -(void) storeImages { NSMutableArray *fuckingArrayYemekName = [[NSMutableArray alloc] init]; for (int i=0; i<[_ResimAdiBase count]; i++) { [fuckingArrayYemekName addObject:[self getCachedImage:_ResimSonAdi[i]]]; } _ResimSonHali = fuckingArrayYemekName; } - (void) cacheImage: (NSString *) ImageURLString : (NSString *)imageName { NSURL *ImageURL = [NSURL URLWithString: ImageURLString]; // Generate a unique path to a resource representing the image you want NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString *docDir = [paths objectAtIndex: 0]; NSString *docFile = [docDir stringByAppendingPathComponent: imageName]; // Check for file existence if(![[NSFileManager defaultManager] fileExistsAtPath: docFile]) { // The file doesn't exist,we should get a copy of it // Fetch image NSData *data = [[NSData alloc] initWithContentsOfURL: ImageURL]; UIImage *image = [[UIImage alloc] initWithData: data]; // Is it PNG or JPG/JPEG? // Running the image representation function writes the data from the image to a file if([ImageURLString rangeOfString: @".png" options: NSCaseInsensitiveSearch].location != NSNotFound) { [UIImagePNGRepresentation(image) writeToFile: docFile atomically: YES]; } else if([ImageURLString rangeOfString: @".jpg" options: NSCaseInsensitiveSearch].location != NSNotFound || [ImageURLString rangeOfString: @".jpeg" options: NSCaseInsensitiveSearch].location != NSNotFound) { [UIImageJPEGRepresentation(image,100) writeToFile: docFile atomically: YES]; } } } - (UIImage *) getCachedImage : (NSString *)imageName { NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString* cachedPath = [documentsDirectory stringByAppendingPathComponent:imageName]; UIImage *image; // Check for a cached version if([[NSFileManager defaultManager] fileExistsAtPath: cachedPath]) { image = [UIImage imageWithContentsOfFile: cachedPath]; // this is the cached image } else { NSLog(@"Error getting image %@",imageName); } return image; }
当我加载20个数据时,我们的表格不会滞后但是当我们尝试增加数据大小时,表格视图会滞后我们如何证明这个问题.首先我们尝试调度然后我们尝试保存图像缓存仍然我们有滞后.大约3天左右,我们处理这个问题.
解决方法
这是cacheImage()方法中的问题行,每次调用“cellForRowAtIndexPath”方法都会调用它
NSData *data = [[NSData alloc] initWithContentsOfURL: ImageURL];
因此,要解决此问题,请在dispatch_async部分下使用此行.并根据它更新您的代码.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。