Quartz 2D 常用简单功能汇总

本人录制技术视频地址:https://edu.csdn.net/lecturer/1899 欢迎观看。

跑马灯

跑马灯, 就是类似于电子屏幕上面无限滚动的广告效果。实现原理很简单,就是开启一个定时器,实时的刷新,即调用[self setNeedsDisplay]进行重绘,如有不理解的,请参考我上一节的博客,请点击这里。跑马灯实现的代码大致如下:

- (void)awakeFromNib {
    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(setNeedsDisplay)];
    
    [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)drawRect:(CGRect)rect {
    CGRect currentRect = self.frame;
    
    currentRect.origin.x++;
    
    if(currentRect.origin.x >= [UIScreen mainScreen].bounds.size.width){
        currentRect.origin.x = -self.frame.size.width;
    }
    
    self.frame = currentRect;
}

这里的定时器最好选用CADisplayLink, 因为刷新频率比较快。


图片水印

效果图:


实现思路,要有一个背景图片,一个水印图片,应该还告诉水印图片的位置,所以设计的代码大致如下:

+ (UIImage *)imageWaterMarkWithBgImage:(NSString *)bg waterImage:(NSString *)waterImage waterImagePosition:(CGPoint)position
{
    UIImage *bgImage = [UIImage imageNamed:bg];
    
    // 获取位图上下文
    UIGraphicsBeginImageContextWithOptions(bgImage.size, NO, 0.0);
    
    // 将图片画到上下文
    [bgImage drawInRect:CGRectMake(0, 0, bgImage.size.width, bgImage.size.height)];
    
    UIImage *logo = [UIImage imageNamed:waterImage];
    CGFloat logoW = logo.size.width;
    CGFloat logoH = logo.size.height;
    CGFloat logoX = bgImage.size.width - logoW - position.x;
    CGFloat logoY = bgImage.size.height - logoH - position.y;
    
    [logo drawInRect:CGRectMake(logoX, logoY, logoW, logoH)];
    
    // 从上下文中取出图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    // 结束上下文
    UIGraphicsEndImageContext();
    
    return newImage;
}
自定义圆角图片(也可以用layer实现)

效果图:


实现思路:1.图片;2.图片边框;3.边框颜色

+ (UIImage *)imageRoundedWithName:(NSString *)name borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor
{
    UIImage *image = [UIImage imageNamed:name];
    
    CGFloat drawLen = image.size.width + borderWidth * 2;
    
    CGFloat bigRadius = drawLen * 0.5;
    CGFloat smallRadius = image.size.width * 0.5;
    CGSize paintSize = CGSizeMake(drawLen, drawLen);
    
    // 确定画图版大小
    UIGraphicsBeginImageContextWithOptions(paintSize, NO, 0.0);
    
    // 获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    // 画圆
    CGContextAddArc(ctx, bigRadius, bigRadius, bigRadius, 0, M_PI * 2, 0);
    
    [borderColor set];
    
    CGContextFillPath(ctx);
    
    // 通过裁切重新定义绘制区域
    CGContextAddArc(ctx, bigRadius, bigRadius, smallRadius, 0, M_PI * 2, 0);
    CGContextClip(ctx);
    
    // 在裁切的圆形区域放置图片(随着borderWidth的改变,裁切圆会等比例缩放)
    [image drawInRect:CGRectMake(borderWidth, borderWidth, image.size.width, image.size.height)];
    
    // 获取上下文中的图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return newImage;
}

屏幕截图

+ (UIImage *)imageWithView:(UIView *)view {
    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0);
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    [view.layer renderInContext:ctx];
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return newImage;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秋恨雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值