I don't know about the black thumbnail. The image is flipped because the CoreGraphics drawing surface' y axis is correct, and UIKit's y axis is inverted (for some weird reason). You will need to translate the grid and scale the image accordingly. Use these two lines:
Code:
CGContextTranslateCTM(UIGraphicsGetCurrentContext, 0.0, CGImageGetHeight(theImage)); //Whereas theImage is a CGImageRef. Using that is much more accurate than feeding in a height variable, but if you must, use a CGFloat variable.
CGContextScaleCTM(UIGraphicsGetCurrentContext, 1.0, -1.0);