好东西小知识点

UIImage以及图形相关

2017-06-01  本文已影响94人  松哥888

绘图和动画

关于贝塞尔曲线和CALayer以及UIView本身的动画,是需要了解的,实际工作中会用到。下面几篇文章比较好,用到的基本都在了。
iOS动画篇_UIBezierPath(贝塞尔曲线)
iOS动画篇_CALayer这些牛逼的子类你造吗
iOS动画篇_UIView动画

设置圆角

imageView.layer.masksToBounds = YES;
imageView.layer.cornerRadius = 10;

在大多数情况下,这么做是可以的。只是这里会触发离屏渲染,降低性能,在表格使用中,会掉帧。这个点是App的性能优化方案之一

解决方法是通过UIGraphicsBeginImageContextWithOptionsUIBezierPath配合,画出这个圆角。下面这几篇文章写得比较详细,值得参考:
设置圆角&避免离屏渲染的几种方法

画图

这里有篇比较好的文章,这里的例子也是从里面摘的
iOS Quart2D绘图之UIGraphicsGetCurrentContext基础。

#import "ProcessView.h"

@interface ProcessView ()

@property (nonatomic, assign) CGFloat progress;

@end

@implementation ProcessView

- (void)updateProcess:(CGFloat)process {
    if (process < 0 || (process > 1)) {
        return;
    }
    self.progress = process;
    [self setNeedsDisplay];
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    // 画圆弧
    CGFloat startA = - M_PI_2;
    CGFloat endA =  - M_PI_2 + self.progress * M_PI * 2;
    NSLog(@"%f - %f - %f",self.progress,(3.14159265359*self.progress)/180,endA);
    UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.bounds.size.width*0.5, self.bounds.size.height*0.5) radius:self.bounds.size.width*0.5-10 startAngle:startA endAngle:endA clockwise:YES];
    UIColor *color = [UIColor colorWithRed:self.progress green:(1-self.progress) blue:0 alpha:1];
    [color setStroke];
    path.lineWidth = 5;
    [path stroke];
    
    // 画文字
    //1.获取当前上下文
    CGContextRef contextRef = UIGraphicsGetCurrentContext();
    //2.创建文字
    NSString * str = [NSString stringWithFormat:@"%04.2f%%", (self.progress * 100)];
    //设置字体样式
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    //NSFontAttributeName:字体大小
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:20];
    //字体前景色
    dict[NSForegroundColorAttributeName] = color;
    //字体背景色
    dict[NSBackgroundColorAttributeName] = [UIColor clearColor];
    //字体阴影
    NSShadow * shadow = [[NSShadow alloc]init];
    //阴影偏移量
    shadow.shadowOffset = CGSizeMake(2, 2);
    //阴影颜色
    shadow.shadowColor = [UIColor greenColor];
    //高斯模糊
    shadow.shadowBlurRadius = 5;
    dict[NSShadowAttributeName] = shadow;
    //字体间距
    dict[NSKernAttributeName] = @10;
    //绘制到上下文
    //从某一点开始绘制 默认 0 0点
    //[str drawAtPoint:CGPointMake(100, 100) withAttributes:dict];
    //绘制区域设置
    CGSize size = [str sizeWithAttributes:dict];
    CGFloat width = size.width;
    CGFloat height = size.height;
    CGFloat x = rect.size.width / 2 - width / 2;
    CGFloat y = rect.size.height / 2 - height / 2;
    [str drawInRect:CGRectMake(x, y, width, height) withAttributes:dict];
    //添加到上下文
    CGContextStrokePath(contextRef);
}

@end

这里是Demo地址

文字水印

// 文字水印
+ (UIImage *)waterWithImage:(UIImage *)image text:(NSString *)text point:(CGPoint)point attributed:(NSDictionary * )attributed {
    //1.开启上下文
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    //2.绘制图片
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    //添加水印文字
    [text drawAtPoint:point withAttributes:attributed];
    //3.从上下文中获取新图片
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    //4.关闭图形上下文
    UIGraphicsEndImageContext();
    //返回图片
    return newImage;
}

图片水印

// 图片水印
+ (UIImage *)waterWithImage:(UIImage *)image mask:(UIImage *)mask rect:(CGRect)rect {
    //1.开启上下文
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    //2.绘制原图
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    //3.绘制水印图片到当前上下文
    [mask drawInRect:rect];
    //4.从上下文中获取新图片
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    //5.关闭图形上下文
    UIGraphicsEndImageContext();
    //返回图片
    return newImage;
}

裁剪圆形图片

+ (UIImage *)clipCircleWithImage:(UIImage *)image {
    //1、开启上下文
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    //2、设置裁剪区域
    UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    [path addClip];
    //3、绘制图片
    [image drawAtPoint:CGPointZero];
    //4、获取新图片
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    //5、关闭上下文
    UIGraphicsEndImageContext();
    //6、返回新图片
    return newImage;
}

设置边框颜色和大小

imageView.layer.borderColor = [UIColor orangeColor].CGColor;
imageView.layer.borderWidth = 2;
// 带边框的圆形裁剪
+ (UIImage *)clipCircleWithImage:(UIImage *)image borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor {
    //1、开启上下文
    UIGraphicsBeginImageContext(image.size);
    
    //2、设置边框
    CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
    UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:rect];
    [borderColor setFill];
    [path fill];
    
    //3、设置裁剪区域
    UIBezierPath * clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(rect.origin.x + borderWidth , rect.origin.x + borderWidth , rect.size.width - borderWidth * 2, rect.size.height - borderWidth *2)];
    [clipPath addClip];
    
    //3、绘制图片
    [image drawAtPoint:CGPointZero];
    
    //4、获取新图片
    UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
    //5、关闭上下文
    UIGraphicsEndImageContext();
    //6、返回新图片
    return newImage;
}

保存图片

Deep Analysis UIImageJPEGRepresentation&UIImagePNGRepresentation

参考文章

以上的例子都来自下面这篇文章
iOS Quart2D绘图之UIGraphicsBeginImageContextWithOptions基础。

图片拉伸的例子来自这里
UIImage常用的分类

IOS-UIImageView

这里是Demo地址
点击button之后的下一页就是Image的变换

上一篇 下一篇

猜你喜欢

热点阅读