Cocoa中常用绘制方法实例代码

//
//  CustomiseView.m
//  CustomiseViewDemo
//
//  Created by Ben on 12-11-29.
//  Copyright (c) 2012年 Ben. All rights reserved.
//

#import "CustomiseView.h"
#import "stdlib.h"

@implementation CustomiseView

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }
   
    return self;
}

- (void)drawRect:(NSRect)rect
{
    //由于绘图是有图层的叠加,可以注释+++++之间的代码以方便查看绘制
   
    //+++++++++++++++++++++++++++NSBezierPath functions++++++++++++++++++
    //----------------------画线-----------------------------
    [[NSColor redColor] set];   //设置当前画布环境中画笔颜色
    NSBezierPath *horizontalPath = [NSBezierPath bezierPath];
    [horizontalPath moveToPoint: NSMakePoint(NSMinX(rect), NSMidY(rect))];
    [horizontalPath lineToPoint: NSMakePoint(NSMaxX(rect), NSMidY(rect))];
    [horizontalPath stroke];     //沿构造路径使用画笔颜色,画线
    NSBezierPath *verticalPath = [NSBezierPath bezierPath];
    [verticalPath moveToPoint: NSMakePoint(NSMidX(rect), NSMinY(rect))];
    [verticalPath lineToPoint: NSMakePoint(NSMidX(rect), NSMaxY(rect))];
    [verticalPath stroke];      //沿构造路径使用画笔颜色,画线
   
    //---------------------画简单的图形------------------------
    NSRect rect1 = NSMakeRect(10, NSMidY(rect)-70, 100, 50);
    NSRect rect2 = NSMakeRect(150,NSMidY(rect)-70, 100, 50);
    NSRect rect3 = NSMakeRect(290,NSMidY(rect)-70, 100, 50);
    CGFloat RADIUS = 10;
    NSBezierPath *path1 = [NSBezierPath bezierPathWithRect: rect1];
    [[NSColor redColor] set];
    [path1 stroke];            //使用当前画笔沿该路径画线
    NSBezierPath *path2 = [NSBezierPath bezierPathWithRoundedRect: rect2 xRadius: RADIUS yRadius: RADIUS];
    [[NSColor blueColor] set];
    [path2 fill];              //使用当前画笔沿填充该路径
    NSBezierPath *path3 = [NSBezierPath bezierPath];
   
    [path3 moveToPoint:NSMakePoint(NSMinX(rect3), NSMinY(rect3) + RADIUS)];
    [path3 lineToPoint:NSMakePoint(NSMinX(rect3), NSMaxY(rect3) - RADIUS)];
    [path3 appendBezierPathWithArcFromPoint:NSMakePoint(NSMinX(rect3), NSMaxY(rect3)) toPoint:NSMakePoint(NSMinX(rect3) + RADIUS, NSMaxY(rect3)) radius:RADIUS];
    [path3 lineToPoint:NSMakePoint(NSMaxX(rect3), NSMaxY(rect3))];
    [path3 lineToPoint:NSMakePoint(NSMaxX(rect3), NSMinY(rect3))];
    [path3 lineToPoint:NSMakePoint(NSMinX(rect3) + RADIUS, NSMinY(rect3))];
    [path3 appendBezierPathWithArcFromPoint:NSMakePoint(NSMinX(rect3), NSMinY(rect3))
                                    toPoint:NSMakePoint(NSMinX(rect3), NSMinY(rect3) + RADIUS)
                                     radius:RADIUS];
   
    NSGradient* aGradient = [[[NSGradient alloc] initWithColorsAndLocations:
                              [NSColor colorWithDeviceRed:140.0/255 green:140.0/255 blue:140.0/255 alpha:1.0], (CGFloat)0,
                              [NSColor colorWithDeviceRed:100.0/255 green:100.0/255 blue:100.0/255 alpha:1.0], (CGFloat)0.48,
                              [NSColor colorWithDeviceRed:70.0/255 green:70.0/255 blue:70.0/255 alpha:1.0], (CGFloat)0.52,
                              [NSColor colorWithDeviceRed:30.0/255 green:30.0/255 blue:30.0/255 alpha:1.0f], (CGFloat)1.0,
                              nil] autorelease];
   
    [aGradient drawInBezierPath: path3 angle: 90];   //使用渐变填充该路径
   

    //++++++++++++++++++++NSString and NSAttributedString functions++++++++++++++++
    NSString *stringToDraw = @"text draw in the center of the view";
    NSMutableDictionary *attrDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                       [NSFont systemFontOfSize: [NSFont systemFontSizeForControlSize: NSRegularControlSize]] ,NSFontAttributeName,
                                       [NSColor blueColor], NSForegroundColorAttributeName, nil];
    NSSize textSize1 = [stringToDraw sizeWithAttributes:attrDict];
    NSPoint pointToDrawString = NSMakePoint(NSMidX(rect) - textSize1.width/2, NSMidY(rect) - textSize1.height/2);
    [stringToDraw drawAtPoint:pointToDrawString withAttributes:attrDict];
    NSAttributedString *attrStringToDraw = [[[NSAttributedString alloc] initWithString:@"text draw in random position in the view" attributes:attrDict] autorelease];
    NSSize textSize2 = [attrStringToDraw size];
    CGFloat randomX = -1, randomY = -1;
    randomX = arc4random() % ((NSUInteger)NSMidX(rect) - 20) + 10;
    randomY = arc4random() % ((NSUInteger)NSMidY(rect) - 20) + 10;
    [attrStringToDraw drawAtPoint:NSMakePoint(randomX, randomY)];
   
    //++++++++++++++++++++NSImage functions+++++++++++++++++++
    NSImage *imageToDraw = [NSImage imageNamed:@"stadio.png"];
 NSRect fromRect = NSZeroRect;
 fromRect.size = [imageToDraw size];
 [imageToDraw drawInRect: rect
                   fromRect: fromRect
      operation: NSCompositeSourceOver
       fraction: 0.3f];       //各参数详细介绍参考帮助文档
   
    NSImage *footballImage = [NSImage imageNamed:@"football.png"];
    NSRect from = NSZeroRect;
    from.size = [footballImage size];
    NSRect toRect = NSMakeRect(0, 0, 50, 50);
    toRect.origin.x = NSMidX(rect) - toRect.size.width/2 - 300;
    toRect.origin.y = NSMidY(rect) - 250;
    [footballImage drawInRect:toRect fromRect:from operation:NSCompositeCopy fraction:1.0f respectFlipped:FALSE hints:nil];
   
    toRect.origin.x += 600;
    [footballImage drawInRect:toRect fromRect:from operation:NSCompositePlusDarker fraction:1.0f respectFlipped:TRUE hints:nil];
   
    //++++++++++++++++++++CoreGraphics functions++++++++++++++++++++
    //-------------------------画线---------------------------------
    CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
    CGContextSetBlendMode(context, kCGBlendModeNormal);
    CGFloat components[4];
    [[NSColor colorWithDeviceRed:0 green:0 blue:0 alpha:1.0f] getComponents:components];
    CGContextSetStrokeColor(context, components);
    CGContextMoveToPoint(context, NSMinX(rect), NSMinY(rect));
    CGContextAddLineToPoint(context, NSMidX(rect), NSMidY(rect));
    CGContextAddLineToPoint(context, NSMaxX(rect), NSMaxY(rect));
    CGContextStrokePath(context);
    CGContextMoveToPoint(context, NSMinX(rect), NSMaxY(rect));
    CGContextAddLineToPoint(context, NSMaxX(rect), NSMinY(rect));
    CGContextStrokePath(context);
   
    //-------------------------画曲线-------------------------------
    CGContextMoveToPoint(context, NSMinX(rect), NSMidY(rect));
    CGContextAddCurveToPoint(context,
                             NSMinX(rect)+300, NSMidY(rect)+300,
                             NSMaxX(rect)-300, NSMidY(rect)-300,
                             NSMaxX(rect), NSMidY(rect));
    CGContextStrokePath(context);
   
    //-------------------------画椭圆-------------------------------
    CGContextSetLineWidth(context, 10);
    CGContextStrokeEllipseInRect(context, rect);

    //-------------------------画矩形-------------------------------
    NSRect squareRect1 = NSMakeRect(10, NSMidY(rect)+20, 100, 50);
    NSRect squareRect2 = NSMakeRect(150, NSMidY(rect)+20, 100, 50);
    NSRect squareRect3 = NSMakeRect(290, NSMidY(rect)+20, 100, 50);
   
    CGContextSetLineWidth(context, 1);
    CGContextStrokeRect(context, squareRect1);
   
    CGContextMoveToPoint(context, NSMinX(squareRect2), NSMinY(squareRect2) + RADIUS);
    CGContextAddLineToPoint(context, NSMinX(squareRect2), NSMaxY(squareRect2) - RADIUS);
    CGContextAddArcToPoint(context, NSMinX(squareRect2), NSMaxY(squareRect2),
                           NSMinX(squareRect2) + RADIUS, NSMaxY(squareRect2), RADIUS);
    CGContextAddLineToPoint(context, NSMaxX(squareRect2) - RADIUS, NSMaxY(squareRect2));
    CGContextAddArcToPoint(context, NSMaxX(squareRect2), NSMaxY(squareRect2),
                           NSMaxX(squareRect2), NSMaxY(squareRect2) - RADIUS, RADIUS);
    CGContextAddLineToPoint(context, NSMaxX(squareRect2), NSMinY(squareRect2) + RADIUS);
    CGContextAddArcToPoint(context, NSMaxX(squareRect2), NSMinY(squareRect2),
                           NSMaxX(squareRect2) - RADIUS, NSMinY(squareRect2), RADIUS);
    CGContextAddLineToPoint(context, NSMinX(squareRect2) + RADIUS, NSMinY(squareRect2));
    CGContextAddArcToPoint(context, NSMinX(squareRect2),  NSMinY(squareRect2),
                           NSMinX(squareRect2), NSMinY(squareRect2) + RADIUS, RADIUS);
    CGContextAddLineToPoint(context, NSMinX(squareRect2), NSMinY(squareRect2) + RADIUS);
    CGContextClosePath(context);
    CGContextFillPath(context);
      
    CGContextMoveToPoint(context, NSMinX(squareRect3), NSMinY(squareRect3) + RADIUS);
    CGContextAddLineToPoint(context, NSMinX(squareRect3), NSMaxY(squareRect3) - RADIUS);
    CGContextAddArcToPoint(context, NSMinX(squareRect3), NSMaxY(squareRect3),
                           NSMinX(squareRect3) + RADIUS, NSMaxY(squareRect3), RADIUS);
    CGContextAddLineToPoint(context, NSMaxX(squareRect3), NSMaxY(squareRect3));
    CGContextAddLineToPoint(context, NSMaxX(squareRect3), NSMinY(squareRect3));
    CGContextAddLineToPoint(context, NSMinX(squareRect3)+RADIUS, NSMinY(squareRect3));
    CGContextAddArcToPoint(context, NSMinX(squareRect3),  NSMinY(squareRect3),
                           NSMinX(squareRect3), NSMinY(squareRect3) + RADIUS, RADIUS);
    CGContextClosePath(context);
    CGContextFillPath(context);
   
    //-------------------------画图片-----------------------------------
    NSRect imageRectToDraw = NSMakeRect( NSMidX(rect) - 325, NSMidY(rect) + 200, 50, 50);
    NSData * imageData = [footballImage TIFFRepresentation];
    CGImageRef imageRef;
    if(imageData)
    {
        CGImageSourceRef imageSource =
        CGImageSourceCreateWithData((CFDataRef)imageData,  NULL);
        imageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
        CGContextSetBlendMode(context, kCGBlendModeCopy);
        CGContextDrawImage(context, imageRectToDraw, imageRef);   //关于图层的颜色叠加算法详细参考帮助文档
        imageRectToDraw.origin.x += 600;
        CGContextSetBlendMode(context, kCGBlendModePlusDarker);
        CGContextDrawImage(context, imageRectToDraw, imageRef);
    }
    CGContextSetBlendMode(context, kCGBlendModeCopy);
   
   
    //-----------------------画图片的一部分-------------------------------
    CGContextSaveGState(context);
    NSRect rectToClip = NSMakeRect( NSMidX(rect) - 50, NSMidY(rect) + 200, 100, 100);
    CGContextAddEllipseInRect(context, rectToClip);
    CGContextClip(context);
    NSData *stdioImageData  = [imageToDraw TIFFRepresentation];
    CGImageRef stdioImageRef;
    if(imageData)
    {
        CGImageSourceRef imageSource =
        CGImageSourceCreateWithData((CFDataRef)stdioImageData,  NULL);
        stdioImageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
        CGContextSetBlendMode(context, kCGBlendModeCopy);
        CGContextDrawImage(context, rectToClip, stdioImageRef);
    }

    CGContextSetBlendMode(context, kCGBlendModeCopy);
    CGContextRestoreGState(context);

    //++++++++++++++++++++++++CGAffineTransform functions++++++++++++++
    //关于下面的处理算法详细见总结文档
    //-------------------------移动-------------------------------------
    CGContextSaveGState(context);
    CGContextTranslateCTM(context, 200, 100);
    NSImage * kidImage = [NSImage imageNamed:@"kid.jpg"];
    NSData * imageData2 = [kidImage TIFFRepresentation];
    CGImageRef imageRef2;
    if (imageData2)
    {
        CGImageSourceRef imageSource =
        CGImageSourceCreateWithData((CFDataRef)imageData2,  NULL);
        imageRef2 = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
        CGContextDrawImage(context, NSMakeRect(0, 0, 100, 100), imageRef2);   //注意这张图片在view中的位置
    }
    CGContextRestoreGState(context);
   
    //-------------------------缩放-------------------------------------
    CGContextSaveGState(context);
    CGContextScaleCTM(context, 2, 2);
    if (imageData2)
    {
        CGContextDrawImage(context, NSMakeRect(200, 100, 100, 100), imageRef2); //注意这张图片在view中的位置和大小
    }
    CGContextRestoreGState(context);
   
    //-------------------------旋转-------------------------------------
    CGContextSaveGState(context);
    CGContextRotateCTM(context, 30.0*3.1415926/180);
    if (imageData2)
    {
        CGContextDrawImage(context, NSMakeRect(0, 0, 100, 100), imageRef2); //注意这张图片在view中的位置和大小
    }
    CGContextRestoreGState(context);
   
   
    //------------------------混合效果------------------------------------
    CGContextTranslateCTM(context, NSWidth(rect), NSHeight(rect));
    CGContextScaleCTM(context, 2, 2);
    CGContextRotateCTM(context, -3.1415926);
    if (imageData2)
    {
        CGContextSetBlendMode(context, kCGBlendModeSourceIn);
        CGContextDrawImage(context, NSMakeRect(100, 100, 100, 100), imageRef2);
    }
}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值