13.Quartz2D

来源:互联网 发布:有线路由器 知乎 编辑:程序博客网 时间:2024/06/05 02:28
    1. Quatz2D可以绘制控件
    2. 图形上下文CGContextRef保存绘图信息绘图状态决定绘制到哪里去
    3.    - (void)drawRect:(CGRect)rect{     }  view有多大rect就有多大

     

    1. 获取图形上下文

    CGContextRefctx = UIGraphicsGetCurrentContext();

    1. 画线

    2.1  确定起点

    CGContextMoveToPoint(ctx, 10, 30);

    2.2  添加一条线

    CGContextAddLineToPoint(ctx, 50, 100);

    2.3 渲染出图像

    CGContextStrokePath(ctx);空心的图像

    CGContextFillPath填充的图形

     

    1. 画矩形 

    CGContextAddRect(ctx, CGRectMake(100, 100, 50 , 50));

     

    1. 设置线条宽度

    CGContextSetLineWidth(ctx, 10);

     

    1. 设置线条颜色

    5.1通过c语言来设置  CGContextSetRGBStrokeColor (ctx, r, g, b, a); 233.0/255.0f

    Stroke只能配合stroke 如果用fill是没有效果的

    CGContextSetRGBFillColor

    5.2通过oc设置

    [[UIColor redColor] setFill];

    [[UIColor redColor] setStroke];

    [[UIColor redColor] set] set是代表都有效果

    1. 设置线条的头圆

    6.1 CGContextSetLineCap(ctx, kCGLineCapRound);

    6.2设置线之间链接线条的节点的样式

    CGContextSetLineJoin(kCGLineJoinRound);

     

    1. 画椭圆

    CGContextAddEllipseInRect(ctx, CGRectMake());

     

    1. 画圆弧

    CGContextAddArc(ctx,圆心x,圆心y,半径,开始的角度,结束的角度,顺时针还是逆时针);

    0顺时针  1逆时针

    Fill只会填充起始和终点,并不会看点

    1. 闭合路径

    CGContextClosePath(ctx);

     

    1. 画文字不需要上下文

    10.1设置文字

    NSString *word = @“我们饿了,要吃饭”;

    10.2画文字

    withAttribute用来表示文字的属性 字典!

    [word drawAtPoint:(CGPoint)withAttributes:();

     

    nsMutableDictionary *dict = [NSMutableDictionary dictionary]

    Dict[NSForegroundColorAttributeName] = [UIColor redColor];

    Dict[NSFontAttributeName] = [UIFont systemFontOfSize:14];

     

    1. [word drawInRect:(cgrect) withAttributes:]

    超出范围不显示自动换行

    [word drawWithRect:  options: attributes]

     

    1. 画图片不需要上下文

    UIImage *image = [UIImage imageNamed:]

    不会拉伸图片

    [image drawAtPoint:CGPointMake(100, 100)];

     

    会根据设定的范围拉伸图片

    [image drawInRect:CGRectMake(10, 10, 100, 100)];

     

    进行平铺展示

    [image drawAsPatternInRect:rect]

     

    1. 贝塞尔曲线

    13.1设置控制点

    13.2设置起始点

    13.3设置终点

    CGContextAddQuadCurveToPoint(ctx,控制点X,控制点Y ,结束X,结束Y)

     

    1. 如果只是绘制线条必须使用stroke

     

    1. 让下一次使用在此是默认样式

    CGContextSaveGState(ctx);

     开启一个上下文栈 copy的上下文放在栈中 保存的样式存在栈中

    渲染图形的效果始终是根据上下文栈顶的上下文来确定

     

    再画第二条线的时候恢复原来的上下文,放在上下文栈中

    CGContextRestoreGState

     

    1. 矩阵旋转操作

     

    矩阵操作要放在图形的上面

    CGContextRotateCTM(ctx, M_PI_4);  旋转操作

     

    上下文的操作缩放

    CGContextScaleCTM(ctx, 0.5, 1);

     

    上下文的平移

    CGContextTranslateCTM(ctx, 100, 200);

     

     

    1. 裁剪当前图片

    绘制图形 之后裁剪图片

    CGContextClip();

     

    1. drawRect: (CGRect)rect:  这个方法不能直接调用

    [self.arcView setNeedsDisplaty];

    [self setNeedsDisplaty];

    [self setNeedsDisplayInRect:];

     

    1. 为什么控件用weak 是因为weak可以不让计数器增加,利于提高性能
    2. 为什么用nonatomic是因为手机端重性能轻安全性
    3. 圆的增大方向是顺时针

    1. Storyboardxib默认是归档在程序中,当使用的时候需要先解档。

    他就会调用 -(instancetype)initWithCoder:(NSCoder *)aDecoder{}

    然后就会转换成代码

    awakeFromNib这个事加载xib的时候调用先解档再调用这个

    1. storyboard的本质是xib
    2. Nstimer通常是每隔一秒以上调用某一个方法的时候使用
    3. CADisplayLink 他可以调用一秒钟调用60
    4. CADisplayLink *display = [CADsiplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)];

    [Display addToRunLoop:[NSRunLoop mainRunLoop] forMode:]

    1. UI层次只有一条线程 图片轮播器只有一条线程
    2. 上下文保存线条的属性具体
    3. C语言ocnilcNULL 

    CGMutablePathpath = CGPathCreateMutable();

    CGPathMoveToPoint (path, NULL, 100 100);

    CGPathAddLineToPoint(path, NULL, 200, 300);

    CGContextAddPath(ctx, path);

    1. arc是编译器特性,是在对应的位置添加retainrelease,autoRelease。

    不过这是针对oc对象,智能管理oc对象的内存

    如果是c语言需要手动管理内存

    只要在c中出现create, copy, retain,都需要释放内存

    CGPathRealease(path);corefundation也可以使用CFRelease;

    1. Analyze是上线的时候,我们需要进行内存检测。
    2. 两步合一步CGContextStrokeRect(ctx, CGRectMake(100, 100, 100, 50));
    3. UIButton *btn = [uibutton buttonwithtype];
    4. UIGraphicsBeginImageContextWithOptions(CGSize size, 不透明 == YES ,是否被压缩 0标示不压缩);
    5. NSArray/NSDictionary/NSString/NSData
    6. 图片要写入文件要转成2进制

    CGFloat compressionQuality:标示图片质量,压缩值

    NSData *imageData  = [UIImageJPEGRepresentation(image, 压缩至)

    NSData *imageData = UIImagePNGRepresentation(image);

    [imageData writeToFile:@""可以拖文件夹  atomically:YES];

    1. 类方法一般比对象方法快方法中没有访问成员变量的时候就可以使用类方法
    2. 抽取工具类方法 要找对应的类 可以写Extension或者Help

     

    1. 截屏

    UIGraphicBeginImageContext(self.view.frame.size);

    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]

    UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();

    UIgraphicsEndImageContext();

    NSData *clipsImageData = UIImagePnGRepresentation(clipImage);

    [clipImageData writeToFile:@"/User --- "automically: YES];

    1. 保存到相册中就不需要转成2进制
    2. UIImageWriteToSavedPhotosAlbum(clipImage, self, @selector(这个点进去有的))

    在这个方法中判断if(error)如果是真就保存失败

    1. 三大事件触摸事件加速计事件 远程控制事件
    2. UIApplication UIVIEWcONTROLLER uivgIEW都继承自UIResponder都能处理事件
    3. touchesBegan touchesMoves touchesEnded touchesCancles
    4. motionBeganmotionEndedmotionCancelled
    5. 点击次数tabCount  触摸了多少秒timestamp
    6. Nsset存储无序的不同的对象

    NSArry存储有序的可以重复的对象

    NSDictionary:用键值对来存储数据

    1. 一次触摸事件获取的手指是一样的
    2. 获取当前触摸点的位置 CGPointcurrentPoint = [touch locationInView:self];

    获取上一次的触摸点的位置[touch previousLocationInView:self];


0 0
原创粉丝点击