#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