UIImage decompression causing scrolling lag

后端 未结 3 1408
执念已碎
执念已碎 2021-01-30 07:26

I have this app with a full screen tableView that displays a bunch of tiny images. Those images are pulled from the web, processed on a background thread, and then saved to disk

3条回答
  •  生来不讨喜
    2021-01-30 08:00

    Your problem is that +imageWithContentsOfFile: is cached and lazy loading. If you want to do something like this, instead use this code on your background queue:

    // Assuming ARC
    NSData* imageFileData = [[NSData alloc] initWithContentsOfFile:thumbPath];
    UIImage* savedImage = [[UIImage alloc] initWithData:imageFileData];
    
    // Dispatch back to main queue and set image...
    

    Now, with this code, the actual decompression of the image data will still be lazy and cost a little bit, but not nearly as much as the file access hit you're getting with the lazy loading in your code example.

    Since you're still seeing a performance issue, you can also force UIImage to decompress the image on the background thread:

    // Still on background, before dispatching to main
    UIGraphicsBeginImageContext(CGSizeMake(100, 100)); // this isn't that important since you just want UIImage to decompress the image data before switching back to main thread
    [savedImage drawAtPoint:CGPointZero];
    UIGraphicsEndImageContext();
    
    // dispatch back to main thread...
    

提交回复
热议问题