UITableView scrolling is not smooth

前端 未结 6 595
囚心锁ツ
囚心锁ツ 2021-02-01 20:41

I have the smooth scrolling issue at my UITableView with UITableViewCell which contains UIImageView. Similar issues could be found all over the StrackOverflow but none of the pr

相关标签:
6条回答
  • 2021-02-01 20:48

    I don't have enough rep to comment, So here's an answer which helped my tableview scrolling performance:

    • Make the tableview height larger than the viewable window. Cells will load "off screen" and helps improve scroll smoothness.
    • Do your image processing in the following method: -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

    Those two tricks got my table flowing really nice. I'm getting my image data from an API service and AFNETWORKING has an awesome image loader, but not necessary for you since images are in the bundle.

    0 讨论(0)
  • 2021-02-01 20:52

    Did you every tried to populate your TableView with only one 120x120 Image which is saved in your Bundle? This way you can check, if the problem occurs of your Image rendering

    Instead of resizing all your images to 120x120 and save them in cache, I would recommend creating and using a thumbnail of all your images. You are somehow already doing this, but you are doing this couple of times (everytime you are scrolling or if your cache is full).

    In our last project we had a UICollectionView with book covers. Most of the covers were between 400-800kb big and the feeling while scrolling was really bad. So we created a thumbnail for each image (thumbails about 40-50kb) and used the thumbnails instead of real covers. Works like a charm! I attached the thumbnail creation function

    - (BOOL) createThumbnailForImageAtFilePath:(NSString *)sourcePath withName:(NSString *)name {
    
        UIImage* sourceImage = [UIImage imageWithContentsOfFile:sourcePath];
        if (!sourceImage) {
            //...
            return NO;
        }
    
        CGSize thumbnailSize = CGSizeMake(128,198);
    
        float imgAspectRatio = sourceImage.size.height / sourceImage.size.width;
        float thumbnailAspectRatio = thumbnailSize.height/thumbnailSize.width;
    
        CGSize scaledSize = thumbnailSize;
    
        if(imgAspectRatio >= thumbnailAspectRatio){
             //image is higher than thumbnail
             scaledSize.width = scaledSize.height * thumbnailSize.width / thumbnailSize.height;
        }
        else{
            //image is broader than thumbnail
            scaledSize.height = scaledSize.width * imgAspectRatio;
        }
    
        UIGraphicsBeginImageContextWithOptions( scaledSize, NO, 0.0 );
        CGRect scaledImageRect = CGRectMake( 0.0, 0.0, scaledSize.width, scaledSize.height );
        [sourceImage drawInRect:scaledImageRect];
        UIImage* destImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        NSString* thumbnailFilePath = [[self SOMEDIRECTORY] stringByAppendingPathComponent:name];
    
       BOOL success = [UIImageJPEGRepresentation(destImage, 0.9) writeToFile:thumbnailFilePath atomically:NO];
    
    return success;
    

    }

    0 讨论(0)
  • 2021-02-01 20:52

    Weel I had a similar problem, my scroll was not smooth. I am inserting in the table a variable UIImageView with inside labelViews. What I did was to change the method HeightforRowAtIndexPath for estimatedHeightforRowAtIndexPath and now scroll is smooth.

    0 讨论(0)
  • 2021-02-01 20:57

    Try facebook's Async Display library.

    https://github.com/facebook/AsyncDisplayKit

    Really easy to use.. from their guide: http://asyncdisplaykit.org/guide/

    _imageNode = [[ASImageNode alloc] init];
    _imageNode.backgroundColor = [UIColor lightGrayColor];
    _imageNode.image = [UIImage imageNamed:@"hello"];
    _imageNode.frame = CGRectMake(10.0f, 10.0f, 40.0f, 40.0f);
    [self.view addSubview:_imageNode.view];
    

    This decodes the image on a background thread.

    I'm not sure if it's easy to use iOS libraries on Xamarin but if it's easy, give this a shot.

    0 讨论(0)
  • 2021-02-01 21:01

    I sub-class Paul Hegarty's CoreDataTableViewController and employ thumbnails of my photos in the CoreDataTableView.

    Look for the examples in Lecture 14 titled FlickrFetcher and Photomania. You will also need to download the CoreDataTableViewController at that same link.

    Make a CoreData Entity with an appropriate title and define whatever attributes (data variables) you want. You will need to define two "Transformable" attributes, one for the photo and one for the thumbnail.

    Then load your thumbnail in the CoreDataTableView:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    

    { NSArray *exceptions = [NSArray arrayWithObjects:@"SCR", @"DNS", @"NT", @"ND", @"NH", nil];

    static NSString *CellIdentifier = @"resultsDisplayCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    MarksFromMeets *athleteMarks = [self.fetchedResultsController objectAtIndexPath:indexPath];
    NSString* date = [ITrackHelperMethods dateToAbbreviatedString:athleteMarks.meetDate];
    NSMutableString *title = [NSMutableString stringWithFormat:@"%@", athleteMarks.markInEvent];
    NSMutableString *subTitle = [NSMutableString stringWithFormat:@"%@ - %@",date, athleteMarks.meetName];
    [title replaceOccurrencesOfString:@"(null)"
                           withString:@""
                              options:0
                                range:NSMakeRange(0, [title length])];
    
    // cell.imageView.image = athleteMarks.photoThumbNail; // Don't like image in front of record.
    
    [cell.textLabel setFont:[UIFont
                             fontWithName:@"Helvetica Neue" size:18]];
    
    [cell.detailTextLabel setFont:[UIFont fontWithName:@"Helvetica Neue" size:16]];
    [cell.detailTextLabel setTextColor:[UIColor grayColor]];
    
    // make selected items orange
    if ([athleteMarks.eventPR integerValue] != 0
        && (![exceptions containsObject:athleteMarks.markInEvent])) {
    
        title = [NSMutableString stringWithFormat:@"%@      (PR)",title];
        [cell.textLabel setTextColor:[UIColor redColor]];
    }
    else if ([athleteMarks.eventSB integerValue] != 0
             && (![exceptions containsObject:athleteMarks.markInEvent])) {
        title = [NSMutableString stringWithFormat:@"%@      (SB)",title];
    
        [cell.textLabel setTextColor:[UIColor orangeColor]];
    } else {
        [cell.textLabel setTextColor:[UIColor grayColor]];
    }
    
    cell.textLabel.text = title;
    cell.detailTextLabel.text = subTitle;
    
    cell.indentationLevel = indentationLevelOne;
    cell.indentationWidth = indentationForCell;
    
    return cell;
    

    }

    If you want, I can send you an example of a Category for an Entity's NSManagedObject Sub-Class. This Category loads the photo and the thumbnail into CoreData Entity. The first time will be slow. However, after that the user should be able to scroll through TableView smoothly and then all the updated results will load automatically. Let me know.

    One nice thing is that CoreData handles all the memory management.

    Good luck!

    0 讨论(0)
  • 2021-02-01 21:02

    Maybe you could try SDWebImage instead. It is also a xamarin component which fashions an asynchronous image downloader and asynchronous memory and disk image caching with automatic cache expiration handling. Using it would probably mean throwing away a lot of hard written code, but it might be worth it -plus your code will become a lot simpler. In iOS you can also setup a SDWebImageManager inside the viewDidLoad of a controller:

    - (void)viewDidLoad{
    ...
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    manager.delegate = self;
    ...
    }
    

    and set the view controller as the delegate. Then, when the following delegate method is called:

    - (UIImage *)imageManager:(SDWebImageManager *)imageManager transformDownloadedImage:(UIImage *)image withURL:(NSURL *)imageURL
    

    you could scale your images to thumbs of the appropriate size before caching them.

    Hope that helps.

    0 讨论(0)
提交回复
热议问题