Quartz2D使用(绘制基本图形)



一 、基本概念atom

图形上下文(Graphics Context):是一个CGContextRef类型的数据spa

图形上下文的做用:保存绘图信息、绘图状态code

只要上下文不一样,绘制的地方就不一样。对象

本文说明如何把图片绘制到Bitmap上面去,即要求生成一张图片,图片上面保存了绘图信息。事件

Bitmap就是图片,至关于系统的UIimage。一个UIImage就是一个Bitmap图片

2、补充说明:ip

1.建立Bitmap图形上下文的方法get

  //方法1   UIGraphicsBeginImageContext(<#CGSize size#>);it

  //方法2 UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale)io

使用两个方法一样均可以建立,可是使用第一个方法未来建立的图片清晰度和质量没有第二种方法的好。


方法2接收三个参数:

CGSize size:指定未来建立出来的bitmap的大小

BOOL opaque:设置透明YES表明透明,NO表明不透明

CGFloat scale:表明缩放,0表明不缩放

建立出来的bitmap就对应一个UIImage对象


3、代码实例

//
//  ViewController.m
//  savePictre
//
//  Created by emily on 16/1/8.
//  Copyright © 2016年 emily. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
}
//点击按钮事件
- (IBAction)btnClick:(UIButton *)sender {
    
    
    // 加载要裁剪的图片
    UIImage * image = [UIImage imageNamed:@"me"];
    
    // 开启一个比图片稍大的图形上下文(bitmap)
    
    CGFloat margin = 5;
    
    CGSize ctxSize = CGSizeMake(image.size.width + 2 * margin, image.size.height + 2 * margin);

    UIGraphicsBeginImageContextWithOptions(ctxSize, NO, 0.0);
    
    //获取刚刚开启的图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
/**绘制一个圆环*/
    //肯定圆心
    CGPoint centerP = CGPointMake(ctxSize.width/2, ctxSize.height/2);
    
    CGFloat radius = MIN(image.size.width, image.size.height)/2;
    
    UIBezierPath * path1 = [UIBezierPath bezierPathWithArcCenter:centerP radius:radius startAngle:0 endAngle:2 * M_PI clockwise:YES];
    
    CGContextAddPath(ctx, path1.CGPath);
    //设置线宽和颜色
    [[UIColor yellowColor]set];
    
    CGContextSetLineWidth(ctx, 5);
    
    //渲染
    CGContextDrawPath(ctx, kCGPathStroke);
/**绘制一个要裁剪的圆形*/
    UIBezierPath * path2 = [UIBezierPath bezierPathWithArcCenter:centerP radius:radius startAngle:0 endAngle:2 * M_PI clockwise:YES];
    
    CGContextAddPath(ctx, path2.CGPath);
    
    CGContextClip(ctx);
    //绘制图片
    [image drawAtPoint:CGPointMake(margin, margin)];
    
    //从图形上下文中获取图片
    UIImage * getImage = UIGraphicsGetImageFromCurrentImageContext();
    //得到屏幕的缩放比
    CGFloat scale = [UIScreen mainScreen].scale;
    // 切割图片
    CGFloat x = 0;
    CGFloat y = (getImage.size.height - 2 * radius)/2;
    CGFloat h =2 * radius ;
    CGFloat w = 2 * radius;
    
    x *= scale;
    y *= scale;
    h *= scale;
    w *= scale;
    
    CGImageRef imageRef = CGImageCreateWithImageInRect(getImage.CGImage, CGRectMake(x, y,w , h));
    
    getImage = [UIImage imageWithCGImage:imageRef];
    //释放
    CGImageRelease(imageRef);
    //关闭图形上下文
    UIGraphicsEndImageContext();
    
    //显示图片
    self.imageView.image = getImage;
    
    //保存相册
    UIImageWriteToSavedPhotosAlbum(getImage, nil, nil, nil);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


   


Emily.Wang

相关文章
相关标签/搜索