/ 绘制一张图片
UIImage *randomBlockImage(CGFloat sideLength, CGFloat inset)
{
UIGraphicsBeginImageContext(CGSizeMake(sideLength, sideLength));
CGContextRef context = UIGraphicsGetCurrentContext();
// Draw in saturated background
CGRect bounds = CGRectMake(0.0f, 0.0f, sideLength, sideLength);
CGContextAddRect(context, bounds);
[[UIColor whiteColor] set];
CGContextFillPath(context);
CGContextAddRect(context, bounds);
[[[UIColor redColor] colorWithAlphaComponent:0.5f] set];
CGContextFillPath(context);
// Draw in brighter foreground
CGContextAddEllipseInRect(context, CGRectInset(bounds, inset, inset));
[[UIColor yellowColor] set];
CGContextFillPath(context);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
我们首先需要设置好绘制图片的上下文环境,利用上图标红的函数,然后就可以利用绘图句柄进行图形绘制;
最后一步将图片从上下文中取出;当然,这里只是一个最简单的使用,如果要绘制出复杂的图片需要进行更加复杂的绘图工作