绘图需在- (void)drawRect:(CGRect)rect方法中实现
第一次显示和调用setNeedsDisplay方法时会调用drawRect方法
//圆环
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 200, 200) cornerRadius:100];
path.lineWidth=10;
[[UIColor redColor] set];
[path stroke];
//扇形
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width*.5, self.bounds.size.height*.5) radius:100 startAngle:0 endAngle:M_PI_2 clockwise:1];
[pathaddLineToPoint:CGPointMake(self.bounds.size.width*.5, self.bounds.size.height*.5)];
[pathclosePath];
path.lineWidth=10;
path.lineCapStyle = kCGLineCapRound;
path.lineJoinStyle = kCGLineCapRound;
[[UIColor redColor] set];
[path stroke];
//文字
NSString *string = @"hello word hello wordhello wordhello wordhello";
[string drawInRect:rect withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:25 weight:5],NSForegroundColorAttributeName:[UIColor redColor]}];
//图片
UIImage*image = [UIImageimageNamed:@"mineral_force"];
[image drawInRect:CGRectMake(100, 100, image.size.width, image.size.height)];
//截图3种方式
//第一种
UIImage *image = [UIImage imageWithCGImage:CGImageCreateWithImageInRect(image.CGImage, CGRectMake(50, 50, 100, 100))];
//第二种
UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 200), false, 0);
[image drawInRect:rect];//[image drawAtPoint:CGPointZero];
self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//第三种
UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 200), false, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddArc(ctx,100,100,80,0,M_PI*2,1);
CGContextClip(ctx);
[imagedrawAtPoint:CGPointMake(0, 0)];
self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//屏幕截图
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[self.view.layer] renderInContext:ctx];
self.subView.image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//水印
[imagedrawAtPoint:CGPointZero];
UIGraphicsBeginImageContextWithOptions(image.size, false, 0);
NSString*string =@"@copyright";
//文字水印
[stringdrawAtPoint:CGPointMake(0, 0) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18 weight:2],NSForegroundColorAttributeName:[UIColor redColor]}];
//图片水印
[[UIImage imageNamed:@"mineral_force"] drawAtPoint:CGPointMake(20, 100)];
self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
网友评论