I have an image that returns everytime the user scrolls the uitableview. This code is inside:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
The code that is being called is the following and creates a shadow for the image specified:
-(UIImage*)imageWithShadowForImageRight:(UIImage *)initialImage {
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef shadowContext = CGBitmapContextCreate(NULL, initialImage.size.width + 10, initialImage.size.height + 10, CGImageGetBitsPerComponent(initialImage.CGImage), 0, colourSpace, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(colourSpace);
CGContextSetShadowWithColor(shadowContext, CGSizeMake(5,-5), 10, [UIColor blackColor].CGColor);
CGContextDrawImage(shadowContext, CGRectMake(0, 10, initialImage.size.width, initialImage.size.height), initialImage.CGImage);
CGImageRef shadowedCGImage = CGBitmapContextCreateImage(shadowContext);
CGContextRelease(shadowContext);
UIImage * shadowedImage = [UIImage imageWithCGImage:shadowedCGImage];
CGImageRelease(shadowedCGImage);
return shadowedImage;
}
The above code makes the scrolling slow and not smooth. Is there a way to overcome this issue by changing the code so it can load the image faster?
Any help appreciated.
You are right, this is the make-or-break function for smooth scrolling. You need to make it as fast as you can by avoiding as much calculation as possible.
For example, you could add caching for the image processed by your
imageWithShadowForImageRight:method: set up an instance ofNSCacheto store processed images; key them by the index path, and avoid recomputation when the same image is requiested multiple times during the process of scrolling.