温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在IOS中使用CALayer绘制图片

发布时间:2021-03-10 16:45:10 来源:亿速云 阅读:295 作者:Leah 栏目:移动开发

本篇文章为大家展示了如何在IOS中使用CALayer绘制图片,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

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

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

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

代码示例:

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中使用CALayer绘制图片,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI