Cocoa in the Shell

[iOS 5] Bug fix for CGImageSourceCreateIncremental

Oct 13, 2011

In a previous post I showed how to create a progressive image download using ImageIO.framework available since iOS 4.
In that post I said that there was a problem concerning the progressive display of non-PNG images because they were malformed.
Fortunately the fix was easy and consisted to simply render the partial image in a bitmap context and get it back, but well, it consumes CPU for nothin'.


The code looked like this :

-(CGImageRef)createTransitoryImage:(CGImageRef)partialImg
{
	const size_t height = CGImageGetHeight(partialImg);
	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
	CGContextRef bmContext = CGBitmapContextCreate(NULL, _fullWidth, _fullHeight, 8, _fullWidth * 4, colorSpace, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
	CGColorSpaceRelease(colorSpace);
	if (!bmContext)
	{
		NSLog(@"fail creating context");
		return NULL;
	}
	CGContextDrawImage(bmContext, (CGRect){.origin.x = 0.0f, .origin.y = 0.0f, .size.width = _fullWidth, .size.height = height}, partialImg);
	CGImageRef goodImageRef = CGBitmapContextCreateImage(bmContext);
	CGContextRelease(bmContext);
	return goodImageRef;
}

And corrected this problem :


Malformed image

It appears to be fixed on iOS 5, we don't need to render other image types in a bitmap context, so it's a pretty good thing.

About • Code