CTM(绘图)

#import "DrawView.h"
#define edg 10
@implementation DrawView
- (void)drawRect:(CGRect)rect
{
//    self.backgroundColor = [UIColor redColor];
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIImage *image = [UIImage imageNamed:@"circle+.png"];
    [image drawInRect:CGRectMake(0, 0, 50, 50)];
    
    CGContextSaveGState(context);//保存图形上下文
    CGContextTranslateCTM(context, 50, 50);//平行当前上下文的变换矩阵
    
    [image drawInRect:CGRectMake(100, 100, 50, 50)];//此图形为平移后的位置+设置的位置
    
    CGContextRestoreGState(context);//恢复图形上下文
    CGContextAddRect(context, CGRectMake(100, 100, 50, 50));
    CGContextDrawPath(context, kCGPathFillStroke);
    
    CGContextSaveGState(context);
    CGContextRotateCTM(context, -M_PI_4);//旋转当前上下文的变换矩阵
    [image drawInRect:CGRectMake(200, 200, 50, 50)];
    
    CGContextRestoreGState(context);
    CGContextSaveGState(context);
    CGContextSetShadow(context, CGSizeMake(5, 5), 2);
    
    //创建透明图层,在Begin和End之间的图形会看作一个整体
    CGContextBeginTransparencyLayer(context, NULL);
    CGContextSetRGBFillColor(context, 1, 0, 0, 1);
    CGContextFillRect(context, CGRectMake(0, 100, 50, 50));
    CGContextSetRGBFillColor(context, 0, 1, 0, 1);
    CGContextFillRect(context, CGRectMake(0, 275, 50, 50));
    CGContextSetRGBFillColor(context, 0, 0, 1, 1);
    CGContextFillEllipseInRect(context, CGRectMake(0, 300, 50, 50));
    CGContextEndTransparencyLayer(context);
    
    //截图
    CGContextRestoreGState(context);
    UIImage *image1 = [UIImage imageNamed:@"18.jpg"];
    [image1 drawInRect:CGRectMake(100, 250, image1.size.width, image1.size.height)];
    CGRect rect1 = CGRectMake(edg, 0, image1.size.width-2*edg, image1.size.height-4*edg);
    CGImageRef dest = CGImageCreateWithImageInRect(image1.CGImage, rect1);//从原始图像截取一个矩形区域
    UIImage *image2 = [UIImage imageWithCGImage:dest];//转化为UIimage对象
    [image2 drawInRect:CGRectMake(200, 0, image2.size.width, image2.size.height)];
    
    }

@end

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值