3

从 NSData 加载图像时,我的 tableview 可以平滑滚动。

一些背景......我将图像 NSData 存储在 Core Data 中(使用“允许外部存储”选项,所以我不认为存储 URL 或类似的东西是这里的解决方案)。此外,我存储了两种类型的数据,一种是高分辨率的 UIImagePNGRepresentation,另一种是低分辨率的 UIImageJPGRepresentation。我正在为我的表格视图加载低分辨率图像。入口对象上的图像属性不存储在核心数据中,它是事后设置的。这是我的做法:

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

    DMLogTableViewCell *cell = (DMLogTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:@"LogCell" forIndexPath:indexPath];

    [cell setGradients:@[[UIColor whiteColor], [UIColor lightTextColor]]];
    Entry *entry = [self.fetchedResultsController objectAtIndexPath:indexPath];
    UIImage *entryImage = entry.image;

    if (entryImage)
        cell.rightImageView.image = entryImage;
    else {
        NSOperationQueue *oQueue = [[NSOperationQueue alloc] init];
        [oQueue addOperationWithBlock:^(void) {
            UIImage *image = [UIImage imageWithData:entry.photo.lowResImageData];
            if (!image) image = [UIImage imageNamed:@"defaultImage.png"];
            entry.image = image;
        }];
        [oQueue addOperationWithBlock:^(void) {
            [[NSOperationQueue mainQueue] addOperationWithBlock:^(void) {
                DMLogTableViewCell *cell = (DMLogTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
                cell.rightImageView.image = entry.image;
            }];
        }];

    }

    return cell;
}

关于如何让它更顺利地运行的任何想法?任何帮助是极大的赞赏!

谢谢

更新

This has gotten me much closer, but there is still a little bit of jerkiness . . . (within cellForRowAtIndexPath)

    UIImage *entryImage = entry.image;

    if (entryImage)
        cell.rightImageView.image = entryImage;
    else {
        [self.imageQueue addOperationWithBlock:^(void) {
            UIImage *image = [UIImage imageWithData:entry.photo.lowResImageData];
            if (!image) image = [UIImage imageNamed:@"bills_moduleIcon.png"];
            entry.image = image;
            [entry.image preload];

            [[NSOperationQueue mainQueue] addOperationWithBlock:^(void) {
                DMLogTableViewCell *cell = (DMLogTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
                cell.rightImageView.image = entry.image;
            }];
        }];
    }

that preload method is in a cateogory on UIImageView and looks like this:

- (void)preload
{
    CGImageRef ref = self.CGImage;
    size_t width = CGImageGetWidth(ref);
    size_t height = CGImageGetHeight(ref);
    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, width * 4, space, kCGImageAlphaPremultipliedFirst);
    CGColorSpaceRelease(space);
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), ref);
    CGContextRelease(context);
}

I think my next plan of attack is to mess around with the size of the images stored (as mentioned in the comments) or perhaps trying to not update the imageViews while the table is scrolling. Any thoughts on those two methods are much appreciated! ill check back in when i've done that

UPDATE 2 Many many thanks for everyone who helped me out with this! Here was the final solution (in cellForRowAtIndexPath:

    if (entryImage)
        cell.rightImageView.image = entryImage;
    else {
        [self.imageQueue addOperationWithBlock:^(void) {
            UIImage *image = [UIImage imageWithData:entry.photo.lowResImageData];
            if (!image) image = [UIImage imageNamed:@"bills_moduleIcon.png"];
            entry.image = [image scaleToSize:cell.rightImageView.frame.size];

            [[NSOperationQueue mainQueue] addOperationWithBlock:^(void) {
                DMLogTableViewCell *cell = (DMLogTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
                cell.rightImageView.image = entry.image;                
            }];
        }];
    }

adding a new category method to UIImage:

-(UIImage*)scaleToSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaledImage;
}

Scrolling is now perfectly smooth. Awesome

4

2 回答 2

2

A couple of issues:

  1. I'd want to see how much time these two lines are taking:

    Entry *entry = [self.fetchedResultsController objectAtIndexPath:indexPath];
    UIImage *entryImage = entry.image;
    

    While I gather that entry.image is a cached image, I wouldn't be so confident that objectAtIndexPath:indexPath isn't actually retrieving the NSData from Core Data (which is a good portion of the performance issue in storing images in a local database rather than the file system). I'd be inclined to do this in the background queue.

  2. I don't understand why you're adding two operations to oQueue (which could operate concurrently!). It should just be as follows, so you don't try to dispatch to main queue until the image is retrieved:

    [oQueue addOperationWithBlock:^(void) {
        UIImage *image = [UIImage imageWithData:entry.photo.lowResImageData];
        if (!image) image = [UIImage imageNamed:@"defaultImage.png"];
        entry.image = image;
    
        [[NSOperationQueue mainQueue] addOperationWithBlock:^(void) {
            DMLogTableViewCell *cell = (DMLogTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
            cell.rightImageView.image = entry.image;
        }];
    }];
    
  3. It's probably not material, but you really shouldn't create a new NSOperationQueue for each cell. You should define a class property that is a NSOperationQueue, initialize it in viewDidLoad, and then use that queue in cellForRowAtIndexPath. It's not material, I'm sure, but it's not worth the overhead of creating a queue for each row of your table. (And if you ever go to server-based image retrieval, using a single queue is very important so you can control the maxConcurrentOperationCount.)

于 2013-01-27T22:33:13.097 回答
0

UIImage wont actually do the decode until it needs to draw the image, so its actually happening on the main thread when you assign the image to the image view.

I have a UIImage category which will force the image to be decoded (which you should run on a background thread) here : https://github.com/tonymillion/TMDiskCache

于 2013-01-27T22:10:23.377 回答