ios-绘制-小知识点(裁减)

-(void)drawRect:(CGRect)rect{
      CGContextRef ctx=UIGraphicsGetCurrentContext();
      CGContextBeginPath(ctx);
      CGContextAddRect(ctx, CGRectMake(100, 100, 100, 100));
      CGContextClip(ctx);//裁减,相对的CGContextClearRect是只留下矩形外面的
      CGContextBeginPath(ctx);
      CGContextAddRect(ctx, CGRectMake(0, 0, rect.size.width,   rect.size.height));
      [[UIColor brownColor]setFill];
      CGContextFillPath(ctx);
}
//绘制路径   
CGMutablePathRef path=  CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 20, 20);
    CFRelease(path);

//
CGContextAddPath(ctx, path1);

#pragma mark --截图
-(void)jitu{
    //切换到图片上下问
    UIGraphicsBeginImageContext(self.frame.size);
    //将view绘制到图形上下文中
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    //将截屏保存到相册
    UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
    
    UIImageWriteToSavedPhotosAlbum(newImage,self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
 {
         if (error) {

             NSLog(@"保存失败,请检查是否拥有相关的权限");
             }else
                 {
                     NSLog(@"保存成功!");
                     }
     }
//绘制图片  
 UIGraphicsBeginImageContextWithOptions( CGSizeMake(200, 200), NO, 0);
             //1.获取bitmap上下文
             CGContextRef ctx = UIGraphicsGetCurrentContext();
             //2.绘图(画一个圆)
             CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 100, 100));
             //3.渲染
             CGContextStrokePath(ctx);
             //4.获取生成的图片
             UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
             //5.显示生成的图片到imageview
//             self.iv.image=image;
             //6.保存绘制好的图片到文件中
             //先将图片转换为二进制数据,然后再将图片写到文件中
         //    UIImageJPEGRepresentation(image, 1); //第二个参数为保存的图片的效果
             NSData *data=UIImagePNGRepresentation(image);
             [data writeToFile:@"/Users/liyang/Library/Developer/abc.png" atomically:YES];

原文地址:https://www.cnblogs.com/liyang31tg/p/4280435.html