UIImage decompression causing scrolling lag

后端 未结 3 1406
执念已碎
执念已碎 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 07:59

    Another way image decompression:

     NS_INLINE void forceImageDecompression(UIImage *image)
     {
      CGImageRef imageRef = [image CGImage];
      CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
      CGContextRef context = CGBitmapContextCreate(NULL, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef), 8, CGImageGetWidth(imageRef) * 4, colorSpace,kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
      CGColorSpaceRelease(colorSpace);
      if (!context) { NSLog(@"Could not create context for image decompression"); return; }
      CGContextDrawImage(context, (CGRect){{0.0f, 0.0f}, {CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)}}, imageRef);
      CFRelease(context);
    }
    

    Using:

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
      UIImage *image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%u.jpg", pageIndex]];
      forceImageDecompression(image);
    
      dispatch_async(dispatch_get_main_queue(), ^{ 
        [((UIImageView*)page)setImage:image];
      });
    }
    

提交回复
热议问题