第一件事是:你不需要推动图形环境。摆脱
UIGraphicsPushContext
和
UIGraphicsPopContext
线。
第二,如何画出你想要的:
-(UIImage*)createCellImageWithCount:(NSInteger)cellCount AndColour:(UIColor*)cellColour {
CGFloat height = IMAGE_HEIGHT;
CGFloat width = IMAGE_WIDTH;
UIImage* inputImage;
UIGraphicsBeginImageContext(CGSizeMake(width, height));
CGContextRef context = UIGraphicsGetCurrentContext();
[cellColour set]; // Set foreground and background color to your chosen color
CGContextFillRect(context,CGRectMake(0,0,width,height)); // Fill in the background
NSString* number = [NSString stringWithFormat:@"%i",cellCount]; // Turn the number into a string
UIFont* font = [UIFont systemFontOfSize:12]; // Get a font to draw with. Change 12 to whatever font size you want to use.
CGSize size = [number sizeWithFont:font]; // Determine the size of the string you are about to draw
CGFloat x = (width - size.width)/2; // Center the string
CGFloat y = (height - size.height)/2;
[[UIColor blackColor] set]; // Set the color of the string drawing function
[number drawAtPoint:CGPointMake(x,y) withFont:font]; // Draw the string
UIImage* outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return outImage;
}