iOS开发中Quartz2D的基本使用方式举例

2020-01-14 17:25:19于海丽

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    // 2.绘图
    CGContextAddRect(ctx, CGRectMake(50, 50, 100, 100));
    // 3.渲染
    CGContextStrokePath(ctx);
    
    
//    NSMutableDictionary *md = [NSMutableDictionary dictionary];
//    // 设置文字颜色
//    md[NSForegroundColorAttributeName] =[UIColor redColor];
//    // 设置文字背景颜色
//    md[NSBackgroundColorAttributeName] = [UIColor greenColor];
//    // 设置文字大小
//    md[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    
    //    将文字绘制到指点的位置
    //    [str drawAtPoint:CGPointMake(10, 10) withAttributes:md];
    
    //    将文字绘制到指定的范围内, 如果一行装不下会自动换行, 当文字超出范围后就不显示
    [str drawInRect:CGRectMake(50, 50, 100, 100) withAttributes:nil];
}


@end


效果:

 

iOS开发中Quartz2D的基本使用方式举例

图片

代码1:

复制代码
//
//  YYimage.m
//  04-写文字
//
//  Created by 孔医己 on 14-6-10.
//  Copyright (c) 2014年 itcast. All rights reserved.
//

 

#import "YYimage.h"

@implementation YYimage


- (void)drawRect:(CGRect)rect
{
 
    //    1.加载图片到内存中
    UIImage *image = [UIImage imageNamed:@"me"];
    
    
    // 利用drawAsPatternInRec方法绘制图片到layer, 是通过平铺原有图片
    [image drawAsPatternInRect:CGRectMake(0, 0, 320, 480)];