//加载图片
UIImage*image=[UIImage imageNamed:@"angry_00.jpg"];
//上下文 : 基于位图image,所有的东西需要绘制到一张新的图片上去
//1.创建一个基于位图的上下文(开启一个基于位图的上下文)
//size : 新图片的尺寸
//opaque : 不透明度
//创建一张新的image,也就是新的UIImage对象
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
//2.画背景
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
//3.画右下角水印图片
UIImage*waterImage=[UIImage imageNamed:@"1.png"];
//设置位置和大小以及显示模式和透明度
[waterImage drawInRect:CGRectMake(image.size.width/2-80, image.size.height/2+80, waterImage.size.width*0.4, waterImage.size.height*0.5)blendMode:kCGBlendModeNormal alpha:0.4];
//4.从上下文中取得制作完毕的UIImange对象
UIImage*newImage=UIGraphicsGetImageFromCurrentImageContext();
//5.结束图片上下文
UIGraphicsEndImageContext();
//6.加载ImageView
UIImageView*imageView=[[UIImageView alloc]initWithFrame:CGRectMake( self.frame.size.width-200,self.frame.size.height-300, 200, 300)];
imageView.image=newImage;
[self addSubview:imageView];
//7.将image对象压缩为PNG格式的二进制对象.
NSData *data = UIImagePNGRepresentation(newImage);
//8.写入文件
// ......
}
//4.绘制饼状图1.0
-(void)drawBing{
CGContextRef context=UIGraphicsGetCurrentContext();
float a=0;
for (float i=1; i<21; i++) {
CGContextAddArc(context, self.frame.size.width/2, self.frame.size.height/2-60, 60,a/20*2*3.14,i/20*2*3.14, 0);
CGContextAddLineToPoint(context, self.frame.size.width/2, self.frame.size.height/2-60);
CGContextClosePath(context);
[[self randomColor] setFill];
CGContextDrawPath(context, kCGPathFillStroke);
a=i;
}
}
//绘制饼状图1.1
-(void)drawPiont{
NSArray *data = @[ @25, @15, @10 , @17 , @3, @10, @20];
// 设置圆心
CGPoint center = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2+100);
// 设置半径
CGFloat r = 60;
// 起始弧度 和 结束弧度
CGFloat start = 0;
CGFloat end = 0;
for (NSInteger i = 0; i <data.count; i++) {
// 结束弧度
end = [data[i]floatValue ] / 100 * 2 * 3.14 + start ;
// 创建路径
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:r startAngle:start endAngle:end clockwise:YES];
// 连接圆心
[path addLineToPoint:center];
// 设置饼的颜色
[[self randomColor] set];
// 渲染
[path fill];
start = end;
}
}
// 随机的颜色
-(UIColor *)randomColor{
UIColor *color = [UIColor colorWithRed:(arc4random_uniform(256)/255.0) green:(arc4random_uniform(256)/255.0) blue:(arc4random_uniform(256)/255.0) alpha:1];
return color;
}
//5.通过传值来绘制圆
-(void)drawCircleWithValue:(float )Number{
[self setNeedsDisplay];
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextAddArc(context, self.frame.size.width/2, self.frame.size.height/2-60, 100,3.14,Number/10*2*3.15+3.14,0);
CGContextSetLineWidth(context, 5);
[[self randomColor] set];
//圆环
//CGContextStrokePath(context);
//扇形图
CGContextAddLineToPoint(context, self.frame.size.width/2, self.frame.size.height/2-60);
CGContextClosePath(context);
CGContextFillPath(context);