I have a CGImage that is displayed in an UIView controlled by a UIScrollView. The image is typically 1680*1050 in 24 bit colors without alpha channel.
The image is created like this:
CGImageRef bitmapClass::CreateBitmap(int width, int height, int imageSize, int colorDepth, int bytesPerRow)
{
unsigned char* m_pvBits = malloc(imageSize);
// Initializt bitmap buffer to black (red, green, blue)
memset(m_pvBits, 0, imageSize);
m_DataProviderRef =
CGDataProviderCreateWithData(NULL, m_pvBits, imageSize, NULL);
m_ColorSpaceRef =
CGColorSpaceCreateDeviceRGB();
return
CGImageCreate(width, height,
8, //kBitsPerComponent
colorDepth,
bytesPerRow,
m_ColorSpaceRef,
kCGBitmapByteOrderDefault | kCGImageAlphaNone,
m_DataProviderRef,
NULL, false, kCGRenderingIntentDefault);
}
The image content is updated regularly in the background by changing the pixels in m_pvBits and updated in the UIView using:
[myView setNeedsDisplayInRect:rect];
That invokes drawRect that displays the image like this:
- (void)drawRect:(CGRect)rect
{
CGRect imageRect;
imageRect.origin = CGPointMake(0.0, 0.0);
imageRect.size = CGSizeMake(self.imageWidth, self.imageHeight);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetInterpolationQuality(context, kCGInterpolationNone);
// Drawing code
CGContextDrawImage(context, imageRect, (CGImageRef)pWindow->GetImageRef());
}
This works well as long as the view is not shrunk (zoomed out). I know that 'rect' is actually not used directly in drawRect but the 'context' seems to know what part of the screen CGContextDrawImage should update.
My problem is that even though I only invalidate a small area of the screen with setNeedsDisplayInRect, drawRect is called with the entire screen when the view is shrunk. So if my image is 1680*1050 and I invalidates a small rectangle (x,y,w,h)=(512, 640, 32, 32), drawRect is called with (x,y,w,h)=(0, 0, 1680, 1050).
Why?