I have to part 50 square images (600px x 600px) into 9 equal-size (200x200) square parts. To get each part i use method:
-(UIImage *)getSubImageFrom:(UIImage *)img WithRect:(CGRect)rect
{
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
// translated rectangle for drawing sub image
CGRect drawRect = CGRectMake(-rect.origin.x, -rect.origin.y, img.size.width, img.size.height);
// clip to the bounds of the image context
// not strictly necessary as it will get clipped anyway?
CGContextClipToRect(context, CGRectMake(0, 0, rect.size.width, rect.size.height));
// draw image
[img drawInRect:drawRect];
// grab image
UIImage* subImage = UIGraphicsGetImageFromCurrentImageContext();
CGContextRelease(context);
return subImage;
}
And it was enough fast for me...since today. I used to load images using [UIImage imageNamed:] method. This method do NOT autorelease memory until app gets memory warning... Which was unacceptable for me.
So i started using [UIImage imageWithContentsOfFile]. Problems with memory allocation disappeared... unfortunately my cropping method (-(UIImage *)getSubImageFrom:(UIImage *)img WithRect:(CGRect)rect) started to work 20 times slower! Its now several hours since I started searching for solution...without result. Hope you can help me.
Best regards!
PS. I've tried to use tips from this question CGContextDrawImage is EXTREMELY slow after large UIImage drawn into it, without any result.