头文件:
#import <UIKit/UIKit.h> #include <math.h> static inline double radians(double degrees) { return degrees * M_PI / 180; } #define FontSize 20.0f @interface DrawScreen : UIView { } - (void)drawTextByString:(NSString *)drawText thePoisition:(CGPoint)point; - (void)drawPicByImage:(UIImage *)theImage theRect:(CGRect)rect; - (void)drawTextByQuartz:(CGContextRef)context theText:(const char *)drawtext thePoisition:(CGPoint)point rotateAngle:(float)angle; - (void)drawPicByQuartz:(CGContextRef)context thePic:(UIImage *)theImage theLocation:(CGRect)rect; @end
实现文件:
#import "DrawScreen.h" @implementation DrawScreen - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { self.backgroundColor = [UIColor blackColor]; } return self; } - (void)drawRect:(CGRect)rect { CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor); //方式一:用NSString的方式画字,用UIImage的方式画图片,坐标原点在左上角 NSString *textOne = [NSString stringWithFormat:@"iPad"]; NSString *textTwo = [NSString stringWithFormat:@"iPhone"]; [self drawTextByString:textOne thePoisition:CGPointMake(50.0f, 260.0f)]; [self drawTextByString:textTwo thePoisition:CGPointMake(250.0f, 260.0f)]; UIImage *flagOne = [UIImage imageNamed:@"flag.png"]; [self drawPicByImage:flagOne theRect:CGRectMake(50.0f, 200.0f, 45.0f, 45.0f)]; [self drawPicByImage:flagOne theRect:CGRectMake(250.0f, 200.0f, 45.0f, 45.0f)]; //方式二:用Quartz2D的方式画字和图片,坐标原点移动到左下角 CGContextTranslateCTM(context, 0, self.bounds.size.height); CGContextScaleCTM(context, 1, -1); UIImage *flagTwo = [UIImage imageNamed:@"location.png"]; [self drawTextByQuartz:context theText:"Apple" thePoisition:CGPointMake(160.0f, 200.0f) rotateAngle:45.0f]; [self drawTextByQuartz:context theText:"iPod" thePoisition:CGPointMake(50.0f, 100.0f) rotateAngle:45.0f]; [self drawTextByQuartz:context theText:"iMac" thePoisition:CGPointMake(250.0f, 100.0f) rotateAngle:45.0f]; [self drawPicByQuartz:context thePic:flagTwo theLocation:CGRectMake(160.0f, 230.0f, 50.0f, 50.0f)]; [self drawPicByQuartz:context thePic:flagTwo theLocation:CGRectMake(50.0f, 130.0f, 50.0f, 50.0f)]; [self drawPicByQuartz:context thePic:flagTwo theLocation:CGRectMake(250.0f, 130.0f, 50.0f, 50.0f)]; } - (void)dealloc { [super dealloc]; } - (void)drawTextByString:(NSString *)drawText thePoisition:(CGPoint)point { //方式一 [drawText drawAtPoint:point withFont:[UIFont systemFontOfSize:FontSize]]; } - (void)drawPicByImage:(UIImage *)theImage theRect:(CGRect)rect { //方式一 [theImage drawInRect:rect]; } - (void)drawTextByQuartz:(CGContextRef)context theText:(const char *)drawtext thePoisition:(CGPoint)point rotateAngle:(float)angle { //方式二 CGContextSelectFont(context, "Helvetica", FontSize, kCGEncodingMacRoman); CGContextSetTextDrawingMode(context, kCGTextFill); CGAffineTransform myTextTransform = CGAffineTransformMakeRotation(radians(angle)); CGContextSetTextMatrix (context, myTextTransform); CGContextShowTextAtPoint(context, point.x, point.y, drawtext, strlen(drawtext)); } - (void)drawPicByQuartz:(CGContextRef)context thePic:(UIImage *)theImage theLocation:(CGRect)rect { //方式二 CGContextDrawImage(context, rect, theImage.CGImage); } @end
示例图: