欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS-Quartz2D画图

程序员文章站 2022-05-14 10:25:54
常说温故而知新,时隔这么久再次看这些基础内容仍然很兴奋!把敲的代码分享出来,希望能对读者提供一丁点启发 /** * 裁剪圆形图片 */ -(void)circleicon{...

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

/**
 *  裁剪圆形图片
 */
-(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);

}