iOS表视图滞后问题,我正在使用调度和保存缓存

iOS表视图滞后问题,我正在使用调度和保存缓存,第1张

概述- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCell
- (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部分下使用此行.并根据它更新您的代码.

总结

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1023415.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存