IOS 中CALayer绘制图片的实例详解

所属分类: 软件编程 / IOS 阅读数: 1398
收藏 0 赞 0 分享

IOS 中CALayer绘制图片的实例详解

CALayer渲染内容图层。与UIImageView相比,不具有事件响应功能,且UIImageView是管理内容。

注意事项:如何使用delegate对象执行代理方法进行绘制,切记需要将delegate设置为nil,否则会导致异常crash。

CALayer绘制图片与线条效果图:

代码示例:

CGPoint position = CGPointMake(160.0, 200.0); 
CGRect bounds = CGRectMake(0.0, 0.0, 150.0, 150.0); 
CGFloat cornerRadius = 150.0 / 2; 
CGFloat borderWidth = 2.0; 
// 阴影层 
CALayer *layerShadow = [[CALayer alloc] init]; 
layerShadow.position = position; 
layerShadow.bounds = bounds; 
layerShadow.cornerRadius = cornerRadius; 
layerShadow.borderWidth = borderWidth; 
layerShadow.borderColor = [UIColor whiteColor].CGColor; 
layerShadow.shadowColor = [UIColor grayColor].CGColor; 
layerShadow.shadowOffset = CGSizeMake(2.0, 1.0); 
layerShadow.shadowOpacity = 1.0; 
layerShadow.shadowRadius = 3.0; 
[self.view.layer addSublayer:layerShadow]; 
// 容器层 
CALayer *layerContant = [[CALayer alloc] init]; 
// 添加到父图层 
[self.view.layer addSublayer:layerContant]; 
// 图层中心点、大小(中心点和大小构成frame) 
layerContant.position = position; 
layerContant.bounds = bounds; 
// 图层背景颜色 
layerContant.backgroundColor = [UIColor redColor].CGColor; 
// 图层圆角半径 
layerContant.cornerRadius = cornerRadius; 
// 图层蒙版、子图层是否剪切图层边界 
//  layerContant.mask = nil; 
layerContant.masksToBounds = YES; 
// 边框宽度、颜色 
layerContant.borderWidth = borderWidth; 
layerContant.borderColor = [UIColor whiteColor].CGColor; 
// 阴影颜色、偏移量、透明度、形状、模糊半径 
//  layerContant.shadowColor = [UIColor grayColor].CGColor; 
//  layerContant.shadowOffset = CGSizeMake(2.0, 1.0); 
//  layerContant.shadowOpacity = 1.0; 
//  CGMutablePathRef path = CGPathCreateMutable();   
//  layerContant.shadowPath = path; 
//  layerContant.shadowRadius = 3.0; 
// 图层透明度 
layerContant.opacity = 1.0; 
// 绘制图片显示方法1 
// 图层形变 
// 旋转(angle转换弧度:弧度=角度*M_PI/180;x上下对换、y左右对换、z先上下对换再左右对换;-1.0~1.0) 
//  layerContant.transform = CATransform3DMakeRotation(M_PI, 0.0, 0.0, 0.0); 
// 缩放(0.0~1.0) 
//  layerContant.transform = CATransform3DMakeScale(0.8, 0.8, 0.8); 
// 移动 
//  layerContant.transform = CATransform3DMakeTranslation(10.0, 1.0, 1.0); 
// 显示内容 
 [layerContant setContents:[UIImage imageNamed:@"header"].CGImage]; 

 绘制图片显示方法2 

layerContant.delegate = self; 
[layerContant setNeedsDisplay]; 
 
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 
{ 
  // 绘图 
  CGContextSaveGState(ctx); 
  // 图形上下文形变,避免图片倒立显示 
  CGContextScaleCTM(ctx, 1.0, -1.0); 
  CGContextTranslateCTM(ctx, 0.0, -150.0); 
  // 图片 
  UIImage *image = [UIImage imageNamed:@"header"]; 
  CGContextDrawImage(ctx, CGRectMake(0.0, 0.0, 150.0, 150.0), image.CGImage); 
  CGContextRestoreGState(cox); 
} 

// 绘制实线、虚线 
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 
{   
  // 绘实线 
  // 线条宽 
  CGContextSetLineWidth(ctx, 1.0); 
  // 线条颜色 
//  CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0); 
  CGContextSetStrokeColorWithColor(ctx, [UIColor greenColor].CGColor); 
  // 方法1 
  // 坐标点数组 
  CGPoint aPoints[2]; 
  aPoints[0] = CGPointMake(10.0, 50.0); 
  aPoints[1] = CGPointMake(140.0, 50.0); 
  // 添加线 points[]坐标数组,和count大小 
  CGContextAddLines(ctx, aPoints, 2); 
  // 根据坐标绘制路径 
  CGContextDrawPath(ctx, kCGPathStroke); 
  // 方法2 
  CGContextSetLineWidth(ctx, 5.0); 
  CGContextSetStrokeColorWithColor(ctx, [UIColor purpleColor].CGColor); 
  CGContextMoveToPoint(ctx, 10.0, 60.0); // 起点坐标 
  CGContextAddLineToPoint(ctx, 140.0, 60.0); // 终点坐标 
  CGContextStrokePath(ctx); // 绘制路径 
   
  // 绘虚线 
  // 线条宽 
  CGContextSetLineWidth(ctx, 2.0); 
  // 线条颜色 
  CGContextSetStrokeColorWithColor(ctx, [UIColor blueColor].CGColor); 
  // 虚线 
  CGFloat dashArray[] = {1, 1, 1, 1}; 
  CGContextSetLineDash(ctx, 1, dashArray, 1); 
  // 起点 
  CGContextMoveToPoint(ctx, 10.0, 100.0); 
  // 终点 
  CGContextAddLineToPoint(ctx, 140.0, 100.0); 
  // 绘制路径 
  CGContextStrokePath(ctx); 
} 
// 内存管理,避免异常crash 
- (void)dealloc 
{ 
  for (CALayer *layer in self.view.layer.sublayers) 
  { 
    if ([layer.delegate isEqual:self]) 
    { 
      layer.delegate = nil; 
    } 
  } 
  NSLog(@"%@ 被释放了~", self); 
} 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多精彩内容其他人还在看

iOS逆向教程之跟踪函数调用详解

这篇文章主要给大家介绍了关于iOS逆向教程之跟踪函数调用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享

iOS App连续闪退时上报crash日志的方法详解

iOS App 有时可能遇到启动必 crash 的绝境:每次打开 App 都闪退,无法正常使用App。下面这篇文章主要给大家介绍了iOS App连续闪退时上报crash日志的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

如何为Xcode添加删除整行、复制整行及在下方新建一行快捷键详解

xcode是苹果公司向开发人员提供的集成开发环境,开发者们经常会使用到,下面这篇文章主要给大家介绍了关于如何为Xcode添加删除整行、复制整行及在下方新建一行快捷键的相关资料,需要的朋友可以参考下。
收藏 0 赞 0 分享

iOS指纹登录(TouchID)集成方案详解

这篇文章主要为大家详细介绍了iOS指纹登录TouchID的集成方案,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS动态验证码实现代码

本文通过实例代码给大家介绍了ios动态验证码的实现方法,代码简单易懂,非常不错,具有参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

iOS模块化开发浅析

本文给大家分析了IOS在模块发开发时候的相关注意点以及简单代码做了分享,有兴趣的朋友参考学习下。
收藏 0 赞 0 分享

iOS中封装.framework及使用的方法详解

这篇文章主要给大家介绍了关于iOS中封装.framework及使用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧。
收藏 0 赞 0 分享

WKWebView、WebView和JS的交互方式详解

这篇文章主要给大家介绍了关于WKWebView、WebView和JS的交互方式,文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享

ios wkwebview离线化加载h5资源解决方案

本篇文章主要介绍了ios wkwebview离线化加载h5资源解决方案,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

iOS实现带指引线的饼状图效果(不会重叠)

饼状图对大家来说应该都不陌生,下面这篇文章主要给大家介绍了关于iOS实现带指引线的饼状图效果(不会重叠)的相关资料,文章通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧。
收藏 0 赞 0 分享
查看更多