iOS绘图系列四:绘制文字和图像CGContextDrawImage,drawInRect:,drawAtPoint:, UIGraphicsBeginImageContext

本文介绍了iOS开发中两种不同的坐标系统:左上为起始点的坐标系统和左下为起始点的坐标系统,并解释了它们在UIKit、Core Animation及Core Graphics框架中的应用。文章还提供了使用UIKit和Core Graphics进行图片和文字绘制的具体实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

绘制图形和文字之前需要了解两个坐标系统.

Upper-left-origin coordinate system (ULO) :左上为起始点的坐标系统,UIKit and Core Animation框架用的是这个坐标系统.

Lower-left-origin coordinate system (LLO) :左下为起始点坐标系统,Core Graphics框架是这个坐标系统.

这两个不同的坐标系统的不同,就必然需要Core Graphics绘制的图片需要上下翻转,才能正常滴在UIView中显示. 此外, UIImage 和 NSString 可以直接绘制,所以不需要像前面绘制圆弧,直线等一样去用UIBezierPath类去绘制了.


实现方法一:通过UIView的drawInRect:方法内用UIKit框架去实现

    [[UIImage imageNamed:@"ship"] drawInRect:CGRectMake(10, 30, 80, 80)];
    [@"这是一个飞船" drawAtPoint:CGPointMake(10, 120) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor blackColor]}];

实现方法二:通过UIView的drawInRect:方法内用Core Graphics框架去实现

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx);
    //翻转起来---上下颠倒
    CGContextTranslateCTM(ctx, 0.0, self.bounds.size.height);
    CGContextScaleCTM(ctx, 1.0, -1.0);
    
    假设想在10,30,80,80的地方绘制,颠倒过来后的Rect应该是 10, self.bounds.size.height - 110, 80, 80
    CGRect imageRect = CGRectMake(10, self.bounds.size.height - 110, 80, 80);
    CGContextDrawImage(ctx, imageRect, [UIImage imageNamed:@"Ship"].CGImage);
    CGContextRestoreGState(ctx);
    
    //字体在iOS7中被废除了,移入CoreText框架中,以后再详细讨论.
    [@"这是一个飞船" drawAtPoint:CGPointMake(10, 120) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor blackColor]}];
方法三:通过 UIGraphicsBeginImageContextWithOptions去方便的创建图片

    UIGraphicsBeginImageContext(CGSizeMake(80, 100));
    
    [[UIImage imageNamed:@"ship"] drawInRect:CGRectMake(0, 0, 80, 80)];
    [@"这是一个飞船" drawAtPoint:CGPointMake(0, 80) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor blackColor]}];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIGraphicsBeginImageContext(CGSizeMake(80, 100));
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx);
    
    //翻转起来---上下颠倒
    CGContextTranslateCTM(ctx, 0.0, 100);
    CGContextScaleCTM(ctx, 1.0, -1.0);
    
    CGRect imageRect = CGRectMake(0, 20, 80, 80);
    CGContextDrawImage(ctx, imageRect, [UIImage imageNamed:@"Ship"].CGImage);
    CGContextRestoreGState(ctx);
    
    //字体在iOS7中被废除了,移入CoreText框架中,以后再详细讨论.
    [@"这是一个飞船" drawAtPoint:CGPointMake(0, 80) withAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14],NSForegroundColorAttributeName:[UIColor blackColor]}];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值