IOS用程式碼繪製一張圖片
阿新 • • 發佈:2019-02-12
/ 繪製一張圖片
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;
}
我們首先需要設定好繪製圖片的上下文環境,利用上圖示紅的函式,然後就可以利用繪圖控制代碼進行圖形繪製;
最後一步將圖片從上下文中取出;當然,這裡只是一個最簡單的使用,如果要繪製出複雜的圖片需要進行更加複雜的繪圖工作