Quartz2D 绘图

2016-05-19  本文已影响68人  reloadRen

绘制文字

- (void)drawRect:(CGRect)rect {
    // Drawing code   
    //绘制文字
    UIBezierPath * path = [UIBezierPath bezierPathWithRect:CGRectMake(50, 50, 200, 200)];

    [path stroke];       
//数据    
NSString * str = @"生活不止眼前的苟且,还有诗和远方的田野";   
    //绘制
    //描述文字信息    
NSDictionary * atts = @{
          NSForegroundColorAttributeName:[UIColor redColor], 
          NSFontAttributeName:[UIFont systemFontOfSize:25.f] 
};       
//绘制的点  参数2:描述文字
//    [str drawAtPoint:CGPointZero withAttributes:atts];//从某点开始绘制    
//把文字绘制到指定区域   
 [str drawInRect:CGRectMake(50, 50, 200, 200) withAttributes:atts];
}

绘制图片

- (void)drawRect:(CGRect)rect {
    // Drawing code 
    //绘制图片       
//1.加载图片    
UIImage * image = [UIImage imageNamed:@"Press"]; 

 //2.绘制
//   [image drawAtPoint:CGPointZero];//从某一点开始绘制,图片有多大就绘制多大
//   [image drawInRect:rect];//绘制到指定区域,图片小就拉伸  图片大就压缩
 
    [image drawAsPatternInRect:rect];//在指定区域平铺  填满整个区域
}

裁剪圆形图片

- (void)drawRect:(CGRect)rect {    
// Drawing code       
//1.获取图形上下文对象    
CGContextRef ctx = UIGraphicsGetCurrentContext();       
//2.绘制圆形路径
UIBezierPath * path= [UIBezierPath bezierPathWithArcCenter:CGPointMake(50, 50) radius:50 startAngle:0 endAngle:2 * M_PI clockwise:YES];
//3.把路径添加到图形上下文中    
CGContextAddPath(ctx, path.CGPath);       
//4.执行裁剪    
CGContextClip(ctx);       
//5.加载图片    
UIImage * image = [UIImage imageNamed:@"me"];   
//6.绘制图片
 [image drawAtPoint:CGPointZero];   
}
上一篇下一篇

猜你喜欢

热点阅读