码迷,mamicode.com
首页 > 移动开发 > 详细

iOS-Quartz2D画图

时间:2016-07-15 11:18:19      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

常说温故而知新,时隔这么久再次看这些基础内容仍然很兴奋!把敲的代码分享出来,希望能对读者提供一丁点启发

/**
 *  裁剪圆形图片
 */
-(void)circleIcon{
    UIImage *image = [UIImage imageNamed:@"baby"];
    UIGraphicsBeginImageContext(image.size);
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    [path addClip];
    [image drawAtPoint:CGPointZero];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    self.imageV.image = newImage;
}

/**
 *  图片加水印
 */
-(void)waterMark{

    UIImage *image= [UIImage imageNamed:@"baby"];

    //开启图片上下文
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);
    //将图片绘制到图片上下文
    [image drawAtPoint:CGPointZero];
    //绘制文字
    NSString *str = @"邱学伟";
    [str drawAtPoint:CGPointZero withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20]}];
    //生成图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //手动关闭上下文
    UIGraphicsEndImageContext();

    self.imageV.image = newImage;

}



-(void)awakeFromNib{
//    [self timer];
}



/**
 *  图形上下文矩阵操作
 */
-(void)drawMatrix{
    //1.开启一个上下文
    CGContextRef CTX = UIGraphicsGetCurrentContext();
    //2.绘制路径
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(100, 100, 200, 100)];
    [[UIColor redColor] set];

    //上下文矩阵操作
    //平移
//    CGContextTranslateCTM(CTX, -50, -50);

    //缩放
    CGContextScaleCTM(CTX, 0.5, 0.5);

    //旋转
    CGContextRotateCTM(CTX, M_PI_4);

    //3.将路径添加到图形上下文
    CGContextAddPath(CTX, path.CGPath);
    //4.把上下水渲染出来
    CGContextFillPath(CTX);
}


/**
 *  定时器下雪
 */
-(void)timer{
    //定时器方法1-卡顿
//    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(updateSnow) userInfo:nil repeats:YES];

    //定时器方法2-在屏幕每次刷新(没秒刷新60次)时调用
    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateSnow)];
    [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

}
-(void)updateSnow{
    _Y+=10;
    if (_Y > self.bounds.size.height) {
        _Y = 0;
    }
    [self setNeedsDisplay];
}
static int _Y = 0;

/**
 *  画图片
 */
-(void)drawImageWith:(CGRect)rect{
    UIImage *image = [UIImage imageNamed:@"snow"];
    //保持图片源尺寸
    [image drawAtPoint:CGPointMake(10, _Y)];
    //填充整个View
//    [image drawInRect:self.bounds];
    //把image画满整个View
//    [image drawAsPatternInRect:self.bounds];
}


/**
 *  画文字
 */
-(void)drawTextWith:(CGRect)rect{
    NSString *str = @"邱学伟";
    NSMutableDictionary *attri = [NSMutableDictionary dictionary];
    [attri setObject:[UIFont systemFontOfSize:50] forKey:NSFontAttributeName];
    [attri setObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    //不会自动换行
    [str drawAtPoint:CGPointZero withAttributes:attri];
    //会自动换行
    [str drawInRect:rect withAttributes:attri];
}

/**
 *  画饼图
 */
-(void)drawPreWithRect:(CGRect)rect{
    NSArray *angleArr = @[@25,@25,@50];
    CGPoint center = CGPointMake(rect.size.width*0.5, rect.size.height*0.5);
    CGFloat radius = rect.size.width*0.5-10;
    CGFloat Angle = 0;
    CGFloat startAngle = 0;
    CGFloat endAngle = 0;

    for (NSNumber *num in angleArr) {
        startAngle = endAngle;
        Angle = num.intValue/100.0*M_PI*2;
        endAngle = startAngle + Angle;
        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];
}


/**
 *  画进度条
 */
-(void)setProgressValue:(CGFloat)progressValue{
    if (_progressValue != progressValue) {
        _progressValue = progressValue;
        [self setNeedsDisplay];
    }
}
-(void)drawProgressWithRect:(CGRect)rect{
    CGPoint center = CGPointMake(rect.size.width*0.5, rect.size.height*0.5);
    CGFloat radius = rect.size.width*0.5-10;
    CGFloat startAngle = -M_PI_2;
    CGFloat endAngle = -M_PI_2 + self.progressValue*M_PI*2;
    UIBezierPath *progressPath = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startAngle endAngle:endAngle clockwise:YES];
    [progressPath stroke];
}

/**
 *  画圆和圆弧
 */
-(void)drawCircleWithRect:(CGRect)rect{
    //1. radius - 圆角半径
    UIBezierPath *circel0 = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(20, 20, 100, 100) cornerRadius:10];
    //    [circel0 stroke];

    UIBezierPath *circle1 = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 20, 100, 100)];
    //    [circle1 fill];

    //画弧
    //Center:圆心
    //radius:半径
    //startAngle:开始角度 0:圆右侧
    //endAngle:结束角度
    //clockwise YES:顺时针  NO:逆时针
    CGPoint center = CGPointMake(rect.size.width*0.5, rect.size.height*0.5);
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(rect.size.width*0.5, rect.size.height*0.5) radius:50 startAngle:0 endAngle:-M_PI_2 clockwise:NO];
    [path addLineToPoint:center];
    //    [path addLineToPoint:CGPointMake(center.x+50, center.y)];
    //上行代码可以使用封闭路径
    [path closePath];
    [path stroke];

}

/**
 *  画矩形
 */
-(void)drawRect{
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 100, 100)];
    CGContextAddPath(contextRef, path.CGPath);
    CGContextStrokePath(contextRef);
}

/**
 *  画圆
 */
-(void)drawQuadCurve{
    CGContextRef textRef = UIGraphicsGetCurrentContext();
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(20, 180)];
    [path addQuadCurveToPoint:CGPointMake(200, 180) controlPoint:CGPointMake(110, 10)];
    CGContextSetLineWidth(textRef, 10);
    CGContextAddPath(textRef, path.CGPath);
    CGContextStrokePath(textRef);
}

/**
 *  画线
 */
-(void)drawLine{
    //1.取得一个跟View相关的上下文
    CGContextRef contextRex = UIGraphicsGetCurrentContext();
    //2.1 描述路径
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    //2.2 设置起点
    [bezierPath moveToPoint:CGPointMake(10, 100)];
    //2.3 设置终点
    [bezierPath addLineToPoint:CGPointMake(100, 20)];
    //2.4再设置一个终点
    [bezierPath addLineToPoint:CGPointMake(100, 100)];

    //*设置路径属性 - 上下文状态
    //宽度
    CGContextSetLineWidth(contextRex, 10);
    //线的连接样式
    //    kCGLineJoinMiter, 默认
    //    kCGLineJoinRound, 圆弧
    //    kCGLineJoinBevel  方形
    CGContextSetLineJoin(contextRex, kCGLineJoinBevel);
    //颜色
    [[UIColor redColor] setStroke];

    //3.把路径添加到上下文
    CGContextAddPath(contextRex, bezierPath.CGPath);
    //4.把上下文显示在View上
    CGContextStrokePath(contextRex);

}

iOS-Quartz2D画图

标签:

原文地址:http://blog.csdn.net/qxuewei/article/details/51913712

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!