UI基础 - Quartz2D 04:矩阵(平移、旋转、缩放) | 剪切 | 截屏
矩阵:平移、旋转、缩放
1 - 对绘制的图形进行平移、缩放、旋转操作
1 - (void)drawRect:(CGRect)rect{ 2 3 // [self makeTranslateCTM]; 4 // [self makeRotateCTM]; 5 // [self makeScaleCTM]; 6 7 } 8 9 // 旋转:CGContextRotateCTM 10 -(void)makeRotateCTM{ 11 12 CGContextRef ctx1 = UIGraphicsGetCurrentContext(); 13 CGContextRotateCTM(ctx1, M_PI_4); // 旋转的时候,是整个 Layer 都做了旋转 14 CGContextAddRect(ctx1, CGRectMake(260, 50, 100, 120)); 15 CGContextStrokePath(ctx1); 16 } 17 18 // 缩放:CGContextAddRect 19 - (void)makeScaleCTM{ 20 21 CGContextRef ctx = UIGraphicsGetCurrentContext(); 22 // 缩放操作根据指定的 x/y 因子来改变坐标空间的大小,从而放大或缩小图像 23 CGContextScaleCTM(ctx, 2, 0.5); 24 CGContextAddRect(ctx, CGRectMake(20, 50, 120, 100)); 25 CGContextAddEllipseInRect(ctx, CGRectMake(22, 152, 50, 50)); 26 CGContextStrokePath(ctx); 27 } 28 29 // 平移:CGContextTranslateCTM 30 - (void)makeTranslateCTM{ 31 32 CGContextRef ctx = UIGraphicsGetCurrentContext(); 33 CGContextTranslateCTM(ctx, 50, 100); 34 CGContextAddRect(ctx, CGRectMake(150, 100, 100, 100)); 35 CGContextAddEllipseInRect(ctx, CGRectMake(200, 200, 50, 50)); 36 CGContextStrokePath(ctx); 37 }
截屏 | 剪切
1 - 在程序开发中,有时候需要截取屏幕上的某一块内容,那么完成截屏功能的核心代码
- (void)renderInContext:(CGContextRef)ctx // 调用某个 view 的 layer 的该方法即可
2 - 代码示例
// - QuartsView.m
1 // 截屏思路 2 // 先绘制区域(这里是圆),让图片显示在圆的内部,超出的部分剪切即可 3 - (void)drawRect:(CGRect)rect{ 4 5 CGContextRef ctx = UIGraphicsGetCurrentContext(); 6 // 指定可以显示图片的范围 7 CGContextAddEllipseInRect(ctx, CGRectMake(100, 210, 110, 110)); 8 // 剪切 9 CGContextClip(ctx); 10 // 绘制图片 11 UIImage *image = [UIImage imageNamed:@"狗子.jpeg"]; 12 [image drawAtPoint:CGPointMake(100, 200)]; 13 }
// - ViewController.m
1 #import "ViewController.h" 2 #import "QuartsView.h" 3 #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width 4 #define SCREENH_HEIGHT [UIScreen mainScreen].bounds.size.height 5 6 @interface ViewController() 7 8 @property(nonatomic,strong)QuartsView *QV; 9 @end 10 11 @implementation ViewController 12 13 - (void)viewDidLoad{ 14 [super viewDidLoad]; 15 16 // 画布 17 self.navigationController.navigationBar.hidden = YES; 18 self.QV = [[QuartsView alloc] initWithFrame:self.view.frame]; 19 self.QV.backgroundColor = [UIColor cyanColor]; 20 [self.view addSubview:self.QV]; 21 22 // 截屏按钮 23 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 24 [btn setTitle:@"截屏" forState:UIControlStateNormal]; 25 btn.backgroundColor = [UIColor redColor]; 26 btn.frame = CGRectMake(40, 80, SCREEN_WIDTH-80, 50); 27 [btn addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside]; 28 [self.view addSubview:btn]; 29 30 } 31 32 // 将图片截入相册/写进文件 33 -(void)click{ 34 35 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 36 37 UIGraphicsBeginImageContext(self.QV.frame.size); 38 // 将 view 绘制到图形里 39 [self.QV.layer renderInContext:UIGraphicsGetCurrentContext()]; 40 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 41 42 // 将图片保存到系统相册 43 UIImageWriteToSavedPhotosAlbum(newImage,self, @selector(image:didFinishSavingWithError:contextInfo:), nil); 44 45 // 将图片写入到文件 46 NSData *data = UIImagePNGRepresentation(newImage); 47 NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"abc.png"]; 48 [data writeToFile:path atomically:YES]; 49 50 }); 51 52 } 53 54 // 提示信息 55 - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{ 56 57 if (error) { 58 NSLog(@"xxx-保存失败"); 59 }else{ 60 NSLog(@"保存成功"); 61 } 62 } 63 64 @end
运行效果
分类:
UI章节
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)