美文网首页
iOS绘图学习

iOS绘图学习

作者: Boy_iOS | 来源:发表于2016-05-22 17:17 被阅读424次

01.绘制线条的第一种方法,最基本的方式

    // 01.获取上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // 02.创建可变路径
    CGMutablePathRef mutablePathRef = CGPathCreateMutable();
    
    // 03.移动到起始点
    CGPathMoveToPoint(mutablePathRef, NULL, 50, 50);
    
    // 04.添加线
    CGPathAddLineToPoint(mutablePathRef, NULL, 200, 200);
    
    // 05.给上线文添加线
    CGContextAddPath(context, mutablePathRef);
    
    // 06.绘制
    CGContextStrokePath(context);

02.绘制线条的第二种方法

    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextMoveToPoint(context, 50, 50);
    
    CGContextAddLineToPoint(context, 200, 200);
    
    CGContextStrokePath(context);

03.绘制一条直线的第三种方式,BezierPath曲线

    // 绘制一条直线的第三种方式,BezierPath曲线
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    [path moveToPoint:CGPointMake(50, 50)];
    [path addLineToPoint:CGPointMake(200, 200)];
    
    [path stroke];

04.图形上下文状态栈

- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // 第一条直线
    UIBezierPath *bezierPath = [UIBezierPath bezierPath];
    [bezierPath moveToPoint:CGPointMake(10, 120)];
    [bezierPath addLineToPoint:CGPointMake(200, 120)];

//    bezierPath.lineWidth = 10;
//    bezierPath.lineCapStyle = kCGLineCapRound;
    
    // 存储绘制状态
    CGContextSaveGState(ctx);
    
    [[UIColor redColor] set];
    CGContextSetLineWidth(ctx, 10);
    
    // 把贝塞尔路径转成上下文路径
    CGContextAddPath(ctx, bezierPath.CGPath);
    
    // 使用上线文绘制直线
    CGContextStrokePath(ctx);
    
//    [bezierPath stroke];
    
    
    
    
    // 第二条直线
    bezierPath = [UIBezierPath bezierPath];
    
    [bezierPath moveToPoint:CGPointMake(120, 10)];
    
    [bezierPath addLineToPoint:CGPointMake(120, 200)];
    
    
//    [[UIColor blackColor] set];
//    CGContextSetLineWidth(ctx, 1);
    // 把贝塞尔路径转成上下文路径
    CGContextAddPath(ctx, bezierPath.CGPath);
    
    // 还原状态
    CGContextRestoreGState(ctx);
    
    // 使用上线文绘制直线
    CGContextStrokePath(ctx);
    
//    [bezierPath stroke];
    
}

05.上下文矩阵操作

// 上下文矩阵操作
- (void)matrixWithContext:(CGContextRef)ctx {
    
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-100, -50, 200, 100)];
    
    // 上下文矩阵操作,必须放在添加路径之前
    CGContextTranslateCTM(ctx, 100, 50);
    
    // 缩放
    CGContextScaleCTM(ctx, 0.5, 0.5);
    
    // 旋转
    CGContextRotateCTM(ctx, M_PI_4);
    
    // 添加路径
    CGContextAddPath(ctx, path.CGPath);
    
    [[UIColor redColor] set];
    
    CGContextFillPath(ctx);
    
//    [[UIColor redColor] set];
//    [path fill];
}

06.生成带水印的图片

#import "UIImage+Extention.h"

@implementation UIImage (Extention)

/** 生成带有水印的图片 */
- (UIImage *)imageAddWatermarkWithText:(NSString *)text
                        withAttributes:(NSDictionary *)attrs
                           drawInPoint:(CGPoint)point{
    
    // 01.获取图片绘制上下文
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0);
    
    // 02.绘制图片
    [self drawAtPoint:CGPointZero];
    
    // 03.绘制文字
    [text drawAtPoint:point withAttributes:attrs];
    
    UIImage *waterImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return waterImage;
}

@end

07.通过路径裁剪图片

/**
 *  通过路径裁剪图片
 *
 *  @param path 图片的裁剪路径(UIBezierPath)
 *
 *  @return 被裁剪的图片
 */
- (UIImage *)imageClipWithPath:(UIBezierPath *)path {
    
    UIGraphicsBeginImageContextWithOptions(self.size, NO, 0);
    
    [path addClip];
    
    [self drawAtPoint:CGPointZero];
    
    UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return clipImage;
}

08.通过已有图片生成带圆环的图片

/**
 *  通过已有图片生成带圆环的图片
 *
 *  @param border      圆环宽度
 *  @param borderColor 圆环颜色
 *
 *  @return 带圆环的图片
 */
- (UIImage *)imageClipWithRingBorder:(CGFloat)border borderColor:(UIColor *)borderColor {
    
    CGFloat imageW = self.size.width;
    CGFloat imageH = self.size.height;
    
    CGFloat ovalW = imageW + 2 * border;
    CGFloat ovalH = imageH + 2 * border;

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(ovalW, ovalH), NO, 0);
    
    UIBezierPath *ovalPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, ovalW, ovalH)];
    
    [borderColor set];
    [ovalPath fill];
    
    UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(border, border, imageW, imageH)];
    
    [clipPath addClip];
    
    [self drawAtPoint:CGPointMake(border, border)];
    
    UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return clipImage;
}
  1. 视图截图
/**
 *  获取视图的截图
 *
 *  @return 截图
 */
- (UIImage *)imageByViewshots {
    
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    [self.layer renderInContext:ctx];
    
    UIImage *screenshots = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return screenshots;
}

10.擦除图片例子

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.view addGestureRecognizer:pan];
}

- (void)pan:(UIPanGestureRecognizer *)pan {
    
    CGPoint curP = [pan locationInView:self.view];
    
    CGFloat wh = 30;
    CGFloat x = curP.x - wh * 0.5;
    CGFloat y = curP.y - wh * 0.5;
    
    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    [self.imgView.layer renderInContext:ctx];
    
    // 擦除上下文内部区域
    CGContextClearRect(ctx, CGRectMake(x, y, wh, wh));
    
    self.imgView.image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();

}

相关文章

  • iOS绘图详解(链接)

    iOS绘图详解iOS绘图教程

  • iOS 绘图

    转自:iOS绘图—— UIBezierPath 和 Core Graphics绘图进阶请参考:绘图 前言 iOS系...

  • iOS绘图框架CoreGraphics分析

    iOS绘图框架CoreGraphics分析 iOS绘图框架CoreGraphics分析

  • iOS绘图学习

    01.绘制线条的第一种方法,最基本的方式 02.绘制线条的第二种方法 03.绘制一条直线的第三种方式,Bezier...

  • Quartz2D

    学习链接 IOS CGContext用法 Quartz2D 绘图 画线的三个步骤: 获取上下文 绘图 渲染 参考链...

  • IOS 学习之绘图( Core Graphics 教学)

    IOS 绘图 总结 Core Graphics IOS中绘图的三种方式 在UIKit控件中,的drawInReat...

  • ios绘图基础

    ios绘图才一些场合很好用,这里演示一些基本的方法。 -1 ios绘图基础 -2 ios常见的图形绘制 代码下载:...

  • iOS 绘图学习实战(一) -- 绘图基础

    1. 使用Quartz的API 创建Context,并且绘图 完全使用Quartz创建Context, 并且使用Q...

  • 绘图

    IOS中绘图的方式介绍 IOS中貌似绘图的方式还挺多的,有 Core Graphics/QuartZ 2D UIK...

  • 绘图方面总结

    学习绘图教程的总结 使用框架:Core Graphics FrameWork iOS支持两套图形API族:Core...

网友评论

      本文标题:iOS绘图学习

      本文链接:https://www.haomeiwen.com/subject/aanorttx.html