HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了iOS表视图滞后问题,我正在使用调度和保存缓存大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
- (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天左右,我们处理这个问题.

解决方法

@H_618_12@ 这是cacheImage()方法中的问题行,每次调用“cellForRowATindexPath”方法都会调用

NSData *data = [[NSData alloc] initWithContentsOfURL: ImageURL];

因此,要解决此问题,请在dispatch_async部分下使用此行.并根据它更新您的代码.

大佬总结

以上是大佬教程为你收集整理的iOS表视图滞后问题,我正在使用调度和保存缓存全部内容,希望文章能够帮你解决iOS表视图滞后问题,我正在使用调度和保存缓存所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。