一. 图片加水印
-(void)watermark{
UIImage *image = self.imageView.image;
//1.第一个参数:大小,第二个参数:不透明度,第三个参数:传入0.0位当前设备分辨率
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);
[image drawAtPoint:CGPointZero];
NSString *text = @"片加水印图片加水印图片加水印图片加水印图片加水印片加水印图片加水印图片加水印图片加水印图片加水印片加水印图片加水印图片加水印图片加水印图片加水印片加水印图片加水印图片加水印图片加水印图片加水印";
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSFontAttributeName] = [UIFont systemFontOfSize:17];
dict[NSForegroundColorAttributeName] = [UIColor redColor];
[text drawInRect:CGRectMake(10, 200, image.size.width, image.size.height) withAttributes:dict];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.imageView.image = image;
}
二. 绘制图片
- (void)drawRect:(CGRect)rect{
[self.image drawInRect:rect];
}
- (void)setImage:(UIImage *)image{
_image = image;
[self setNeedsDisplay];
}
+ (instancetype)initWithImage:(UIImage *)image{
ZQImageView *imageView = [[ZQImageView alloc]init];
imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
imageView.image = image;
return imageView;
}
三. 绘制文字
- (void)drawRect:(CGRect)rect{
NSString *text = @"然后屏幕会从缓存区获取了新的一帧图像并显示出来,与此同时,CPU也开始了下一帧数据的计算";
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//设置字体大小
dict[NSFontAttributeName] = [UIFont systemFontOfSize:20];
//设置字体前景色
dict[NSForegroundColorAttributeName] = [UIColor redColor];
//设置描边颜色
dict[NSStrokeColorAttributeName] = [UIColor blackColor];
//设置描边线宽
dict[NSStrokeWidthAttributeName] = @1;
NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowColor = [UIColor grayColor];
shadow.shadowOffset = CGSizeMake(5, 5);
shadow.shadowBlurRadius = 3;
//设置阴影
dict[NSShadowAttributeName] = shadow;
//[text drawAtPoint:CGPointMake(0, 0) withAttributes:dict];//此方法文字不会换行
[text drawInRect:rect withAttributes:dict];//此方法文字会自动换行
//也可以绘制,也会自动换行
//[text drawWithRect:rect options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
}
四.绘制简单图形
-(void)drawLine:(CGRect)rect{
CGContextRef ctx= UIGraphicsGetCurrentContext();
UIBezierPath *path = [UIBezierPath bezierPath];
//第一条线
[path moveToPoint:CGPointMake(100, 100)];//起点
[path addLineToPoint:CGPointMake(rect.size.width-100, rect.size.height-100)];//终点
// 第二条线
[path moveToPoint:CGPointMake(rect.size.width-100, 100)];//起点
[path addLineToPoint:CGPointMake(100, rect.size.height-100)];//终点
[path addLineToPoint:CGPointMake(120, rect.size.height*0.5+30)];//第三条线,起点为第二条线终点
// 设置线宽
CGContextSetLineWidth(ctx, 10);
//设置拐角样式
CGContextSetLineCap(ctx, kCGLineCapRound);
//设置连接点样式
CGContextSetLineJoin(ctx, kCGLineJoinRound);
//设置线条颜色
[[UIColor redColor]set];
//线条添加到上下文
CGContextAddPath(ctx, path.CGPath);
//绘制到view上
CGContextStrokePath(ctx);
}
-(void)drawClub:(CGRect)rect{
CGContextRef ctx= UIGraphicsGetCurrentContext();
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(10, rect.size.height*0.5)];
[path addQuadCurveToPoint:CGPointMake(rect.size.width-10, rect.size.height*0.5) controlPoint:CGPointMake(rect.size.width*0.5,20)];
CGContextAddPath(ctx, path.CGPath);
CGContextStrokePath(ctx);
}
- (void)drawRect:(CGRect)rect{
UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
CGFloat radius = MIN(rect.size.width, rect.size.height)*0.5-10;
CGPoint center = CGPointMake(rect.size.width*0.5, rect.size.height*0.5);
[[UIColor redColor]set];
[path stroke];
path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:-M_PI_2 endAngle:-M_PI_2+_progress*M_PI*2 clockwise:YES];
[path addLineToPoint:center];
[path fill];
}
- (void)setProgress:(CGFloat)progress{
_progress = progress;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect{
NSArray * array = @[@25,@25,@50,@35,@15,@45,@100];
CGFloat sum = 0;
for (NSNumber *num in array) {
sum += num.intValue*1.0f;
}
CGFloat startAngle = 0;
CGFloat endAngle = 0;
for (NSNumber *num in array) {
endAngle = startAngle + num.intValue /sum *M_PI*2;
[self drawPan:rect startAngle:startAngle endAngle:endAngle];
startAngle = endAngle;
}
}
-(void)drawPan:(CGRect)rect startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle {
CGPoint center = CGPointMake(rect.size.width*0.5, rect.size.height*0.5);
CGFloat radius = MIN(rect.size.width, rect.size.height)*0.5-20;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
[path addLineToPoint:center];
[[self randomColor] set];
[path fill];
}
-(UIColor*)randomColor{
CGFloat r = arc4random_uniform(256)/255.0;
CGFloat g = arc4random_uniform(256)/255.0;
CGFloat b = arc4random_uniform(256)/255.0;
return [UIColor colorWithRed:r green:g blue:b alpha:1.0];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self setNeedsDisplay];
}
-(void)drawRect:(CGRect)rect{
CGRect tempRect = CGRectMake(rect.size.width*0.5+10, 80, rect.size.width*0.5-20, rect.size.height*0.5-110);
UIBezierPath * path = [UIBezierPath bezierPathWithRect:tempRect];
[[UIColor redColor]set];
[path stroke];
}
-(void)drawCircle:(CGRect)rect{
CGFloat radius = MIN(rect.size.width, rect.size.height)*0.5-20;
CGRect tempRect = CGRectMake(10, rect.size.height*0.5+10, radius, radius);
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:tempRect cornerRadius:radius];
[[UIColor redColor]set];
[path stroke];
}
-(void)drawRoundRect:(CGRect)rect{
CGRect tempRect = CGRectMake(10, 80, rect.size.width*0.5-20, rect.size.height*0.5-110);
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:tempRect cornerRadius:10];
[[UIColor redColor]set];
[path stroke];
}
-(void)drawFan:(CGRect)rect{
CGFloat radius = MIN(rect.size.width-20, rect.size.height-20)*0.25;
CGPoint center = CGPointMake(rect.size.width*0.5+5+radius, rect.size.height*0.5+radius+10);
UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:M_PI_2 clockwise:YES];
[[UIColor redColor]set];
[path addLineToPoint:center];
[path stroke];
}
- (void)drawRect:(CGRect)rect{
UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
[[UIColor redColor]set];
[path stroke];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect cgRect = CGRectMake(10, 10, rect.size.width-20, 100);
path = [UIBezierPath bezierPathWithOvalInRect:cgRect];
//平移
CGContextTranslateCTM(ctx, 50, 50);
//旋转
CGContextRotateCTM(ctx, M_PI_4);
//缩放
CGContextScaleCTM(ctx, 0.5, 0.5);
CGContextAddPath(ctx, path.CGPath);
CGContextFillPath(ctx);
}
- 定时器雪花下落效果
static float y = 0;
- (instancetype)initWithFrame:(CGRect)frame{
if(self = [super initWithFrame:frame]){
//[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(changeValue) userInfo:nil repeats:YES];
CADisplayLink *line = [CADisplayLink displayLinkWithTarget:self selector:@selector(changeValue)];
[line addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
return self;
}
-(void)changeValue{
y+=6;
if(y>=[UIScreen mainScreen].bounds.size.height){
y = 0;
}
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect{
UIImage *image = [UIImage imageNamed:@"雪花"];
[image drawAtPoint:CGPointMake(rect.size.width*0.5-image.size.width*0.5, y+image.size.height)];
}