首页 > 其他分享 >iOS开发基础110-Core Graphics应用场景

iOS开发基础110-Core Graphics应用场景

时间:2024-07-17 12:18:52浏览次数:11  
标签:Core end iOS UIColor 110 context Graphics size

Core Graphics是一种强大的二维图形绘制框架,广泛应用于iOS开发中。以下是几个常见的运用场景以及对应的代码示例:

1. 自定义视图绘制

通过覆盖UIView的drawRect:方法,可以自定义视图的外观。

示例代码:

#import <UIKit/UIKit.h>

@interface CustomView : UIView

@end

@implementation CustomView

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    // Set fill color
    CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
    
    // Draw a filled rectangle
    CGContextFillRect(context, CGRectMake(20, 20, 100, 100));
    
    // Set stroke color
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    
    // Draw a circle
    CGContextStrokeEllipseInRect(context, CGRectMake(150, 20, 100, 100));
}

@end

2. 绘制图像

使用Core Graphics可以在视图中绘制图像,并进行一些基本的图像处理操作。

示例代码:

#import <UIKit/UIKit.h>

@interface ImageView : UIView

@end

@implementation ImageView

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIImage *image = [UIImage imageNamed:@"example.png"];
    
    // Draw image in the center of the view
    CGRect imageRect = CGRectMake((self.bounds.size.width - image.size.width) / 2,
                                  (self.bounds.size.height - image.size.height) / 2,
                                  image.size.width,
                                  image.size.height);
    CGContextDrawImage(context, imageRect, image.CGImage);
}

@end

3. 绘制文本

使用Core Graphics可以自定义文本的绘制,包括设置字体、颜色、对齐方式等。

示例代码:

#import <UIKit/UIKit.h>

@interface TextView : UIView

@end

@implementation TextView

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
    
    NSString *text = @"Hello, Core Graphics!";
    UIFont *font = [UIFont systemFontOfSize:24];
    NSDictionary *attributes = @{NSFontAttributeName: font, NSForegroundColorAttributeName: [UIColor blackColor]};
    
    CGSize textSize = [text sizeWithAttributes:attributes];
    CGRect textRect = CGRectMake((self.bounds.size.width - textSize.width) / 2,
                                 (self.bounds.size.height - textSize.height) / 2,
                                 textSize.width,
                                 textSize.height);
    [text drawInRect:textRect withAttributes:attributes];
}

@end

4. 绘制渐变

通过Core Graphics可以绘制线性或径向渐变。

示例代码:

#import <UIKit/UIKit.h>

@interface GradientView : UIView

@end

@implementation GradientView

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    NSArray *colors = @[(__bridge id)[UIColor redColor].CGColor,
                        (__bridge id)[UIColor blueColor].CGColor];
    
    CGFloat locations[] = { 0.0, 1.0 };
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)colors, locations);
    
    CGPoint startPoint = CGPointMake(0, 0);
    CGPoint endPoint = CGPointMake(self.bounds.size.width, self.bounds.size.height);
    
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
    
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorSpace);
}

@end

5. 绘制路径

使用Core Graphics可以创建复杂路径,包括直线、曲线等。

示例代码:

#import <UIKit/UIKit.h>

@interface PathView : UIView

@end

@implementation PathView

- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor);
    CGContextSetLineWidth(context, 2.0);
    
    CGContextMoveToPoint(context, 20, 20);
    CGContextAddLineToPoint(context, 200, 20);
    CGContextAddCurveToPoint(context, 200, 70, 50, 70, 50, 120);
    CGContextAddArc(context, 100, 100, 50, 0, M_PI, 0);
    
    CGContextStrokePath(context);
}

@end

标签:Core,end,iOS,UIColor,110,context,Graphics,size
From: https://www.cnblogs.com/chglog/p/18307049

相关文章

  • iOS开发基础109-网络安全
    在iOS开发中,保障应用的网络安全是一个非常重要的环节。以下是一些常见的网络安全措施及对应的示例代码:Swift版1.使用HTTPS确保所有的网络请求使用HTTPS协议,以加密数据传输,防止中间人攻击。示例代码:在Info.plist中配置AppTransportSecurity(ATS):<key>NSAppTransportSecur......
  • NetCore消息管道 Middleware
    中间件定义///<summary>///自定义中间件1///</summary>publicclassMyMiddleware:IMiddleware{publicasyncTaskInvokeAsync(HttpContextcontext,RequestDelegatenext){Console.WriteLine("MyMiddleware_1,开始处理");aw......
  • Asp .Net Core 系列:基于 T4 模板生成代码
    目录简介组成部分分类VisualStudio中使用T4模板创建T4模板文件2.编写T4模板3.转换模板中心控制Manager根据MySQL数据生成实体简介T4模板,即TextTemplateTransformationToolkit,是微软官方在VisualStudio中引入的一种代码生成引擎。自VisualStudio2008开始,T4模板就被......
  • ASP.NET Core 集成 AutoMapper 的介绍和实际开发应用
    目录一、介绍二、原理三、实际开发应用1.安装AutoMapper包2.创建映射配置3.初始化AutoMapper4.使用AutoMapper进行数据映射5.注意事项和拓展四、总结一、介绍AutoMapper是一个对象-对象映射器(OOM),它主要用于在.NET应用程序中实现对象之间的自动转换。在ASP.NET......
  • dotnet netcore web api 部署 IIS windows 部署 windows server
    .NetCore部署IIS一、服务器环境配置1.1安装ASP.NETCore模块/托管捆绑包ASP.NETCore不再是由IIS工作进程(w3wp.exe)托管,而是使用自托管Web服务器(Kestrel)运行,为了能部署在IIS,必须先安装AspNetCoreModuleV2。安装方式一:安装ASP.NETCore运行时(推荐)下载地址:htt......
  • iOS开发基础108-常见的编程范式
    1.面向过程编程(Process-OrientedProgramming,POP)代码示例(Swift)importUIKitclassViewController:UIViewController{overridefuncviewDidLoad(){super.viewDidLoad()printGreeting()printNumber(num:42)}/......
  • iOS开发基础107-直播
    在iOS平台上,直播技术已经很成熟,有许多强大的第三方框架可以帮助开发者轻松实现直播功能。当前主流的直播第三方框架包括但不限于:LFLiveKit:一款开源的直播推流SDK。PLMediaStreamingKit:由云天存提供的一站式音视频解决方案。AliyunPlayer:阿里云提供的音视频播放解决方案。A......
  • iOS开发基础105-Xcode收集Crashs的各种方法
    Xcode提供了一整套工具和功能来帮助开发者收集、分析和处理应用崩溃报告。通过这些工具,开发者可以追踪和解析崩溃日志,以更加准确和及时地修复问题。以下是详细介绍Xcode工具收集崩溃报告的各种方法。一、通过设备获取崩溃报告1.连接设备将iOS设备通过USB连接到您的Mac......
  • iOS开发基础106-Instruments
    Instruments是苹果公司提供的一款强大的分析和性能调试工具,集成在Xcode开发环境中。Instruments可帮助开发者分析和优化iOS、macOS应用,检测性能瓶颈、内存泄漏、CPU使用率、磁盘I/O等问题,从而提升应用的效率和用户体验。以下详细介绍Instruments的主要功能和使用方法......
  • iOS开发基础104-正向代理和反向代理
    正向代理和反向代理是计算机网络中两种重要的技术,它们在网络请求的传递和管理上扮演着不同的角色。下面将详细介绍这两者的概念、优缺点,并探讨它们在iOS开发中的应用。一、正向代理1.概念正向代理是一种代理服务器,客户端向代理服务器发送请求,由代理服务器转发请求到目标服务器......