原文链接: http://www.cocoachina.com/industry/20140115/7703.html
- - (void) drawRect: (CGRect) rect {
- UIBezierPath* p = [UIBezierPathbezierPathWithOvalInRect:CGRectMake(0,0,100,100)];
- [[UIColor blueColor] setFill];
- [p fill];
- }
- - (void) drawRect: (CGRect) rect {
- CGContextRef con = UIGraphicsGetCurrentContext();
- CGContextAddEllipseInRect(con, CGRectMake(0,0,100,100));
- CGContextSetFillColorWithColor(con, [UIColor blueColor].CGColor);
- CGContextFillPath(con);
- }
- @interface MyLayerDelegate : NSObject
- @end
- @implementation MyLayerDelegate
- - (void)drawLayer:(CALayer*)layer inContext:(CGContextRef)ctx {
- UIGraphicsPushContext(ctx);
- UIBezierPath* p = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,100,100)];
- [[UIColor blueColor] setFill];
- [p fill];
- UIGraphicsPopContext();
- }
- @end
- @interface MyView () {
- MyLayerDelegate* _layerDeleagete;
- }
- @end
- MyView *myView = [[MyView alloc] initWithFrame: CGRectMake(0, 0, 320, 480)];
- CALayer *myLayer = [CALayer layer];
- _layerDelegate = [[MyLayerDelegate alloc] init];
- myLayer.delegate = _layerDelegate;
- [myView.layer addSublayer:myLayer];
- [myView setNeedsDisplay]; // 调用此方法,drawLayer: inContext:方法才会被调用。
- - (void)drawLayer:(CALayer*)lay inContext:(CGContextRef)con {
- CGContextAddEllipseInRect(con, CGRectMake(0,0,100,100));
- CGContextSetFillColorWithColor(con, [UIColor blueColor].CGColor);
- CGContextFillPath(con);
- }
- UIGraphicsBeginImageContextWithOptions(CGSizeMake(100,100), NO, 0);
- UIBezierPath* p = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0,100,100)];
- [[UIColor blueColor] setFill];
- [p fill];
- UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- UIGraphicsBeginImageContextWithOptions(CGSizeMake(100,100), NO, 0);
- CGContextRef con = UIGraphicsGetCurrentContext();
- CGContextAddEllipseInRect(con, CGRectMake(0,0,100,100));
- CGContextSetFillColorWithColor(con, [UIColor blueColor].CGColor);
- CGContextFillPath(con);
- UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- UIImage* mars = [UIImage imageNamed:@"Mars.png"];
- CGSize sz = [mars size];
- UIGraphicsBeginImageContextWithOptions(CGSizeMake(sz.width*2, sz.height), NO, 0);
- [mars drawAtPoint:CGPointMake(0,0)];
- [mars drawAtPoint:CGPointMake(sz.width,0)];
- UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- UIImageView* iv = [[UIImageView alloc] initWithImage:im];
- [self.window.rootViewController.view addSubview: iv];
- iv.center = self.window.center;
- UIImage* mars = [UIImage imageNamed:@"Mars.png"];
- CGSize sz = [mars size];
- UIGraphicsBeginImageContextWithOptions(CGSizeMake(sz.width*2, sz.height*2), NO, 0);
- [mars drawInRect:CGRectMake(0,0,sz.width*2,sz.height*2)];
- [mars drawInRect:CGRectMake(sz.width/2.0, sz.height/2.0, sz.width, sz.height) blendMode:kCGBlendModeMultiply alpha:1.0];
- UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- UIImage* mars = [UIImage imageNamed:@"Mars.png"];
- CGSize sz = [mars size];
- UIGraphicsBeginImageContextWithOptions(CGSizeMake(sz.width/2.0, sz.height), NO, 0);
- [mars drawAtPoint:CGPointMake(-sz.width/2.0, 0)];
- UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- UIImage* mars = [UIImage imageNamed:@"Mars.png"];
- // 抽取图片的左右半边
- CGSize sz = [mars size];
- CGImageRef marsLeft = CGImageCreateWithImageInRect([mars CGImage],CGRectMake(0,0,sz.width/2.0,sz.height));
- CGImageRef marsRight = CGImageCreateWithImageInRect([mars CGImage],CGRectMake(sz.width/2.0,0,sz.width/2.0,sz.height));
- // 将每一个CGImage绘制到图形上下文中
- UIGraphicsBeginImageContextWithOptions(CGSizeMake(sz.width*1.5, sz.height), NO, 0);
- CGContextRef con = UIGraphicsGetCurrentContext();
- CGContextDrawImage(con, CGRectMake(0,0,sz.width/2.0,sz.height), marsLeft);
- CGContextDrawImage(con, CGRectMake(sz.width,0,sz.width/2.0,sz.height), marsRight);
- UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- // 记得释放内存,ARC在这里无效
- CGImageRelease(marsLeft);
- CGImageRelease(marsRight);
- CGImageRef flip (CGImageRef im) {
- CGSize sz = CGSizeMake(CGImageGetWidth(im), CGImageGetHeight(im));
- UIGraphicsBeginImageContextWithOptions(sz, NO, 0);
- CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, sz.width, sz.height), im);
- CGImageRef result = [UIGraphicsGetImageFromCurrentImageContext() CGImage];
- UIGraphicsEndImageContext();
- return result;
- }
- CGContextDrawImage(con, CGRectMake(0,0,sz.width/2.0,sz.height), flip(marsLeft));
- CGContextDrawImage(con, CGRectMake(sz.width,0,sz.width/2.0,sz.height), flip(marsRight));
- UIImage* mars = [UIImage imageNamed:@"Mars.png"];
- CGSize sz = [mars size];
- // 转换CGImage并使用对应的CGImage尺寸截取图片的左右部分
- CGImageRef marsCG = [mars CGImage];
- CGSize szCG = CGSizeMake(CGImageGetWidth(marsCG), CGImageGetHeight(marsCG));
- CGImageRef marsLeft = CGImageCreateWithImageInRect(marsCG,CGRectMake(0,0,szCG.width/2.0,szCG.height));
- CGImageRef marsRight = CGImageCreateWithImageInRect(marsCG, CGRectMake(szCG.width/2.0,0,szCG.width/2.0,szCG.height));
- UIGraphicsBeginImageContextWithOptions(CGSizeMake(sz.width*1.5, sz.height), NO, 0);
- //剩下的和之前的代码一样,修复倒置问题
- CGContextRef con = UIGraphicsGetCurrentContext();
- CGContextDrawImage(con, CGRectMake(0,0,sz.width/2.0,sz.height),flip(marsLeft));
- CGContextDrawImage(con, CGRectMake(sz.width,0,sz.width/2.0,sz.height),flip(marsRight));
- UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- CGImageRelease(marsLeft);
- CGImageRelease(marsRight);
- UIImage* mars = [UIImage imageNamed:@"Mars.png"];
- CGSize sz = [mars size];
- CGImageRef marsCG = [mars CGImage];
- CGSize szCG = CGSizeMake(CGImageGetWidth(marsCG), CGImageGetHeight(marsCG));
- CGImageRef marsLeft = CGImageCreateWithImageInRect(marsCG, CGRectMake(0,0,szCG.width/2.0,szCG.height));
- CGImageRef marsRight = CGImageCreateWithImageInRect(marsCG, CGRectMake(szCG.width/2.0,0,szCG.width/2.0,szCG.height));
- UIGraphicsBeginImageContextWithOptions(CGSizeMake(sz.width*1.5, sz.height), NO, 0);
- [[UIImage imageWithCGImage:marsLeft scale:[mars scale] orientation:UIImageOrientationUp] drawAtPoint:CGPointMake(0,0)];
- [[UIImage imageWithCGImage:marsRight scale:[mars scale] orientation:UIImageOrientationUp] drawAtPoint:CGPointMake(sz.width,0)];
- UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- CGImageRelease(marsLeft); CGImageRelease(marsRight);
- UIImage* moi = [UIImage imageNamed:@"Mars.jpeg"];
- CIImage* moi2 = [[CIImage alloc] initWithCGImage:moi.CGImage];
- CIFilter* grad = [CIFilter filterWithName:@"CIRadialGradient"];
- CIVector* center = [CIVector vectorWithX:moi.size.width / 2.0 Y:moi.size.height / 2.0];
- // 使用setValue:forKey:方法设置滤镜属性
- [grad setValue:center forKey:@"inputCenter"];
- // 在指定滤镜名时提供所有滤镜键值对
- CIFilter* dark = [CIFilter filterWithName:@"CIDarkenBlendMode" keysAndValues:@"inputImage", grad.outputImage, @"inputBackgroundImage", moi2, nil];
- CIContext* c = [CIContext contextWithOptions:nil];
- CGImageRef moi3 = [c createCGImage:dark.outputImage fromRect:moi2.extent];
- UIImage* moi4 = [UIImage imageWithCGImage:moi3 scale:moi.scale orientation:moi.imageOrientation];
- CGImageRelease(moi3);
- - (void)drawRect:(CGRect)rect {
- CGContextRef ctx = UIGraphicsGetCurrentContext();
- CGContextSaveGState(ctx);
- {
- // 绘图代码
- }
- CGContextRestoreGState(ctx);
- }
- CGContextRef con = UIGraphicsGetCurrentContext();
- CGContextSetFillColorWithColor(con, [UIColor blueColor].CGColor);
- CGContextFillRect(con, rect);
- CGContextClearRect(con, CGRectMake(0,0,30,30));
- CGContextRef con = UIGraphicsGetCurrentContext();
- // 绘制一个黑色的垂直黑色线,作为箭头的杆子
- CGContextMoveToPoint(con, 100, 100);
- CGContextAddLineToPoint(con, 100, 19);
- CGContextSetLineWidth(con, 20);
- CGContextStrokePath(con);
- // 绘制一个红色三角形箭头
- CGContextSetFillColorWithColor(con, [[UIColor redColor] CGColor]);
- CGContextMoveToPoint(con, 80, 25);
- CGContextAddLineToPoint(con, 100, 0);
- CGContextAddLineToPoint(con, 120, 25);
- CGContextFillPath(con);
- // 从箭头杆子上裁掉一个三角形,使用清除混合模式
- CGContextMoveToPoint(con, 90, 101);
- CGContextAddLineToPoint(con, 100, 90);
- CGContextAddLineToPoint(con, 110, 101);
- CGContextSetBlendMode(con, kCGBlendModeClear);
- CGContextFillPath(con);
- UIBezierPath* p = [UIBezierPath bezierPath];
- [p moveToPoint:CGPointMake(100,100)];
- [p addLineToPoint:CGPointMake(100, 19)];
- [p setLineWidth:20];
- [p stroke];
- [[UIColor redColor] set];
- [p removeAllPoints];
- [p moveToPoint:CGPointMake(80,25)];
- [p addLineToPoint:CGPointMake(100, 0)];
- [p addLineToPoint:CGPointMake(120, 25)];
- [p fill];
- [p removeAllPoints];
- [p moveToPoint:CGPointMake(90,101)];
- [p addLineToPoint:CGPointMake(100, 90)];
- [p addLineToPoint:CGPointMake(110, 101)];
- [p fillWithBlendMode:kCGBlendModeClear alpha:1.0];
- - (void)drawRect:(CGRect)rect {
- CGContextRef ctx = UIGraphicsGetCurrentContext();
- CGContextSetStrokeColorWithColor(ctx, [UIColor blackColor].CGColor);
- CGContextSetLineWidth(ctx, 3);
- UIBezierPath *path;
- path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 100, 100, 100) byRoundingCorners:(UIRectCornerTopLeft |UIRectCornerTopRight) cornerRadii:CGSizeMake(10, 10)];
- [path stroke];
- }
- CGContextRef con = UIGraphicsGetCurrentContext();
- // 在上下文裁剪区域中挖一个三角形状的孔
- CGContextMoveToPoint(con, 90, 100);
- CGContextAddLineToPoint(con, 100, 90);
- CGContextAddLineToPoint(con, 110, 100);
- CGContextClosePath(con);
- CGContextAddRect(con, CGContextGetClipBoundingBox(con));
- // 使用奇偶规则,裁剪区域为矩形减去三角形区域
- CGContextEOClip(con);
- // 绘制垂线
- CGContextMoveToPoint(con, 100, 100);
- CGContextAddLineToPoint(con, 100, 19);
- CGContextSetLineWidth(con, 20);
- CGContextStrokePath(con);
- // 画红色箭头
- CGContextSetFillColorWithColor(con, [[UIColor redColor] CGColor]);
- CGContextMoveToPoint(con, 80, 25);
- CGContextAddLineToPoint(con, 100, 0);
- CGContextAddLineToPoint(con, 120, 25);
- CGContextFillPath(con);
- CGContextRef con = UIGraphicsGetCurrentContext();
- CGContextSaveGState(con);
- // 在上下文裁剪区域挖一个三角形孔
- CGContextMoveToPoint(con, 90, 100);
- CGContextAddLineToPoint(con, 100, 90);
- CGContextAddLineToPoint(con, 110, 100);
- CGContextClosePath(con);
- CGContextAddRect(con, CGContextGetClipBoundingBox(con));
- CGContextEOClip(con);
- //绘制一个垂线,让它的轮廓形状成为裁剪区域
- CGContextMoveToPoint(con, 100, 100);
- CGContextAddLineToPoint(con, 100, 19);
- CGContextSetLineWidth(con, 20);
- // 使用路径的描边版本替换图形上下文的路径
- CGContextReplacePathWithStrokedPath(con);
- // 对路径的描边版本实施裁剪
- CGContextClip(con);
- // 绘制渐变
- CGFloat locs[3] = { 0.0, 0.5, 1.0 };
- CGFloat colors[12] = {
- 0.3,0.3,0.3,0.8, // 开始颜色,透明灰
- 0.0,0.0,0.0,1.0, // 中间颜色,黑色
- 0.3,0.3,0.3,0.8 // 末尾颜色,透明灰
- };
- CGColorSpaceRef sp = CGColorSpaceCreateDeviceGray();
- CGGradientRef grad = CGGradientCreateWithColorComponents (sp, colors, locs, 3);
- CGContextDrawLinearGradient(con, grad, CGPointMake(89,0), CGPointMake(111,0), 0);
- CGColorSpaceRelease(sp);
- CGGradientRelease(grad);
- CGContextRestoreGState(con); // 完成裁剪
- // 绘制红色箭头
- CGContextSetFillColorWithColor(con, [[UIColor redColor] CGColor]);
- CGContextMoveToPoint(con, 80, 25);
- CGContextAddLineToPoint(con, 100, 0);
- CGContextAddLineToPoint(con, 120, 25);
- CGContextFillPath(con);
- CGColorSpaceRef sp2 = CGColorSpaceCreatePattern(NULL);
- CGContextSetFillColorSpace (con, sp2);
- CGColorSpaceRelease (sp2);
- CGPatternCallbacks callback = {0, &drawStripes, NULL };
- CGAffineTransform tr = CGAffineTransformIdentity;
- CGPatternRef patt = CGPatternCreate(NULL,CGRectMake(0,0,4,4), tr, 4, 4, kCGPatternTilingConstantSpacingMinimalDistortion, true, &callback);
- CGFloat alph = 1.0;
- CGContextSetFillPattern(con, patt, &alph);
- CGPatternRelease(patt);
- void drawStripes (void *info, CGContextRef con) {
- // assume 4 x 4 cell
- CGContextSetFillColorWithColor(con, [[UIColor redColor] CGColor]);
- CGContextFillRect(con, CGRectMake(0,0,4,4));
- CGContextSetFillColorWithColor(con, [[UIColor blueColor] CGColor]);
- CGContextFillRect(con, CGRectMake(0,0,4,2));
- }
- - (void)drawRect:(CGRect)rect {
- UIGraphicsBeginImageContextWithOptions(CGSizeMake(40,100), NO, 0.0);
- CGContextRef con = UIGraphicsGetCurrentContext();
- CGContextSaveGState(con);
- CGContextMoveToPoint(con, 90 - 80, 100);
- CGContextAddLineToPoint(con, 100 - 80, 90);
- CGContextAddLineToPoint(con, 110 - 80, 100);
- CGContextMoveToPoint(con, 110 - 80, 100);
- CGContextAddLineToPoint(con, 100 - 80, 90);
- CGContextAddLineToPoint(con, 90 - 80, 100);
- CGContextClosePath(con);
- CGContextAddRect(con, CGContextGetClipBoundingBox(con));
- CGContextEOClip(con);
- CGContextMoveToPoint(con, 100 - 80, 100);
- CGContextAddLineToPoint(con, 100 - 80, 19);
- CGContextSetLineWidth(con, 20);
- CGContextReplacePathWithStrokedPath(con);
- CGContextClip(con);
- CGFloat locs[3] = { 0.0, 0.5, 1.0 };
- CGFloat colors[12] = {
- 0.3,0.3,0.3,0.8,
- 0.0,0.0,0.0,1.0,
- 0.3,0.3,0.3,0.8
- };
- CGColorSpaceRef sp = CGColorSpaceCreateDeviceGray();
- CGGradientRef grad = CGGradientCreateWithColorComponents (sp, colors, locs, 3);
- CGContextDrawLinearGradient (con, grad, CGPointMake(89 - 80,0), CGPointMake(111 - 80,0), 0);
- CGColorSpaceRelease(sp);
- CGGradientRelease(grad);
- CGContextRestoreGState(con);
- CGColorSpaceRef sp2 = CGColorSpaceCreatePattern(NULL);
- CGContextSetFillColorSpace (con, sp2);
- CGColorSpaceRelease (sp2);
- CGPatternCallbacks callback = {0, &drawStripes, NULL };
- CGAffineTransform tr = CGAffineTransformIdentity;
- CGPatternRef patt = CGPatternCreate(NULL,CGRectMake(0,0,4,4),tr,4,4,kCGPatternTilingConstantSpacingMinimalDistortion,true, &callback);
- CGFloat alph = 1.0;
- CGContextSetFillPattern(con, patt, &alph);
- CGPatternRelease(patt);
- CGContextMoveToPoint(con, 80 - 80, 25);
- CGContextAddLineToPoint(con, 100 - 80, 0);
- CGContextAddLineToPoint(con, 120 - 80, 25);
- CGContextFillPath(con);
- UIImage* im = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- con = UIGraphicsGetCurrentContext();
- [im drawAtPoint:CGPointMake(0,0)];
- for (int i=0; i<3; i++) {
- CGContextTranslateCTM(con, 20, 100);
- CGContextRotateCTM(con, 30 * M_PI/180.0);
- CGContextTranslateCTM(con, -20, -100);
- [im drawAtPoint:CGPointMake(0,0)];
- }
- }
- CGContextTranslateCTM(con, 0, theHeight);
- CGContextScaleCTM(con, 1.0, -1.0);
- CGContextTranslateCTM(con, 0, sz.height); // sz为[mars size]
- CGContextScaleCTM(con, 1.0, -1.0);
- CGContextDrawImage(con, CGRectMake(0, 0, sz.width/2.0, sz.height), marsLeft);
- CGContextDrawImage(con, CGRectMake(b.size.width-sz.width/2.0, 0, sz.width/2.0, sz.height),marsRight);
- con = UIGraphicsGetCurrentContext();
- CGContextSetShadow(con, CGSizeMake(7, 7), 12);
- [im drawAtPoint:CGPointMake(0,0)];
- con = UIGraphicsGetCurrentContext();
- CGContextSetShadow(con, CGSizeMake(7, 7), 12);
- CGContextBeginTransparencyLayer(con, NULL);
- [im drawAtPoint:CGPointMake(0,0)];
- for (int i=0; i<3; i++) {
- CGContextTranslateCTM(con, 20, 100);
- CGContextRotateCTM(con, 30 * M_PI/180.0);
- CGContextTranslateCTM(con, -20, -100);
- [im drawAtPoint:CGPointMake(0,0)];
- }
- // 在调用了CGContextEndTransparencyLayer函数之后,
- // 图层内容会在应用全局alpha和上下文阴影状态之后被合成到上下文中
- CGContextEndTransparencyLayer(con);
- CGContextFillRect(con, CGRectMake(100,0,1.0/self.contentScaleFactor,100));
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- self.window.rootViewController = [UIViewController new];
- MyView* mv =[[MyView alloc] initWithFrame:CGRectMake(0, 0, self.window.bounds.size.width - 50, 150)];
- mv.center = self.window.center;
- [self.window.rootViewController.view addSubview: mv];
- mv.opaque = NO;
- mv.tag = 111; // so I can get a reference to this view later
- [self performSelector:@selector(resize:) withObject:nil afterDelay:0.1];
- self.window.backgroundColor = [UIColor whiteColor];
- [self.window makeKeyAndVisible];
- return YES;
- }