iOS  两张图片合并成一张。 - Go语言中文社区

iOS  两张图片合并成一张。



(一)

UIImage * icon = self.groupIconView.image;//[UIImage imageNamed:@""]; 

  //1.png的图大小为底图

       

      //CGImageRef imgRef = icon.CGImage;

        // CGFloat w = CGImageGetWidth(imgRef);

         //CGFloat h = CGImageGetHeight(imgRef);

  UIImage *img1 = [UIImage imageNamed:@"bg_"];

       // CGImageRef imgRef1 = img1.CGImage;

         //CGFloat w1 = CGImageGetWidth(imgRef1);

         //CGFloat h1 = CGImageGetHeight(imgRef1);

        //1.png的图大小为画布创建上下文

        UIGraphicsBeginImageContext(CGSizeMake(100, 100));

        [img1 drawInRect:CGRectMake(0, 0, 100, 100)];//先把1.png 画到上下文中

        [icon drawInRect:CGRectMake(4, 4, 92, 92)];//再把小图放在上下文中

        UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();//从当前上下文中获得最终图片

        UIGraphicsEndImageContext();//关闭上下文

        

        //        CGImageRelease(imgRef);

        //        CGImageRelease(imgRef1);

        

        icon = resultImg;




应用场景   二维码中间的图标加边框 等等: 

 // 生成带图片的二维码

    [UIImage qrImageWithString:content size:myWidth * qrScale iconImage:icon scale: margin/(myWidth * qrScale) completion:^(UIImage *image) {

        weakSelf.qrImageView.image = image;

}



#pragma mark - 带图片二维码(图片指定比例、方形)

+ (void)qrImageWithString:(NSString *)string  size:(CGFloat)size iconImage:(UIImage *)iconImage scale:(CGFloat)scale completion:(void (^)(UIImage * image))completion {

    // 传入 CenterImgType_Square scale

    

    [UIImage qrImageWithString:string size:size CenterImageType:CenterImgType_CornorRadious iconImage:iconImage scale:scale completion:completion];//CenterImgType_Square 方形 CenterImgType_CornorRadious 切圆角

 

}



(二)


如果内置小图片要求为圆角,则需要绘图


//绘图圆角


        CGRect rect = (CGRect){0.f, 0.f, icon.size};

        

        UIGraphicsBeginImageContextWithOptions(icon.size, NO, UIScreen.mainScreen.scale);

        

        CGContextAddPath(UIGraphicsGetCurrentContext(),

                         [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:8].CGPath);

        

        CGContextClip(UIGraphicsGetCurrentContext());

        

        [icon drawInRect:rect];

        

        UIImage *output = UIGraphicsGetImageFromCurrentImageContext();

        

        UIGraphicsEndImageContext();

 

        

扩展      贝塞尔曲线学习


关于绘制这块,系统已经提供了几个内置的方法:

+ (instancetype)bezierPathWithRect:(CGRect)rect;
+ (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius; // rounds all corners with the same horizontal and vertical radius
+ (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;
+ (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
+ (instancetype)bezierPathWithCGPath:(CGPathRef)CGPath;

今天就简单的介绍一下这几个方法


1:画矩形
  • (instancetype)bezierPathWithRect:(CGRect)rect;
    系统方法,画矩形
    rect: 矩形的Frame
- (void)drawRect:(CGRect)rect
{
    // 设置线的填充色
    [[UIColor redColor] setStroke];

    // 新建一个bezier对象,此对象用于绘制矩形,需要传入绘制的矩形的Frame
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 280, 280)];
    // 设置线宽度
    bezierPath.lineWidth = 10;
    // 设置线两头样式
    bezierPath.lineCapStyle = kCGLineCapRound;

    // 开始绘制
    [bezierPath stroke];
}
iOS <wbr> <wbr>两张图片合并成一张。
屏幕快照 2015-12-30 14.49.09.png

2:画矩形,圆角矩形
  • (instancetype)bezierPathWithRoundedRect:(CGRect)rect cornerRadius:(CGFloat)cornerRadius; // rounds all corners with the same horizontal and vertical radius
    系统方法,绘制一个圆角的矩形
    rect: 矩形的Frame
    cornerRadius: 圆角的半径
- (void)drawRect:(CGRect)rect
{
    // 设置线的填充色
    [[UIColor redColor] setStroke];

    // 新建一个bezier对象,此对象用于绘制一个圆角矩形
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 280, 280)
                                                          cornerRadius:30];
    // 设置线宽度
    bezierPath.lineWidth = 10;

    // 开始绘制
    [bezierPath stroke];
}
iOS <wbr> <wbr>两张图片合并成一张。
屏幕快照 2015-12-30 15.08.32.png

3:画矩形,部分圆角的矩形

  • (instancetype)bezierPathWithRoundedRect:(CGRect)rect byRoundingCorners:(UIRectCorner)corners cornerRadii:(CGSize)cornerRadii;
    画一个部分圆角的矩形
    rect: 需要画的矩形的Frame
    corners: 哪些部位需要画成圆角
    cornerRadii: 圆角的Size
- (void)drawRect:(CGRect)rect
{
    // 设置线的填充色
    [[UIColor redColor] setStroke];

    // 新建一个bezier对象,此对象用于绘制一个部分圆角的矩形,左上、右下
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 280, 280)
                                                     byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight
                                                           cornerRadii:CGSizeMake(10, 10)];
    // 设置线宽度
    bezierPath.lineWidth = 10;

    // 开始绘制
    [bezierPath stroke];
}
iOS <wbr> <wbr>两张图片合并成一张。
屏幕快照 2015-12-30 15.35.48.png

4:画圆,内切圆
  • (instancetype)bezierPathWithOvalInRect:(CGRect)rect;
    画圆,这个方法绘制的是一个矩形的内切圆
    rect: 矩形的Frame
- (void)drawRect:(CGRect)rect
{
    // 设置线的填充色
    [[UIColor redColor] setStroke];

    // 新建一个bezier对象,此对象用于绘制内切圆,需要传入绘制内切圆的矩形的Frame
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 280, 280)];
    // 设置线宽度
    bezierPath.lineWidth = 10;
    // 设置线两头样式
    bezierPath.lineCapStyle = kCGLineCapRound;

    // 开始绘制
    [bezierPath stroke];
}
iOS <wbr> <wbr>两张图片合并成一张。
屏幕快照 2015-12-30 14.57.05.png

5:画圆弧
  • (instancetype)bezierPathWithArcCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise;
    center: 圆心坐标
    radius: 圆的半径
    startAngle: 绘制起始点角度
    endAngle: 绘制终点角度
    clockwise: 是否顺时针
- (void)drawRect:(CGRect)rect
{
    // 设置线的填充色
    [[UIColor redColor] setStroke];

    // 新建一个bezier对象,此对象用于绘制一个圆弧
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(150, 150)
                                                              radius:110
                                                          startAngle:0
                                                            endAngle:M_PI_2
                                                           clockwise:NO];
    // 设置线宽度
    bezierPath.lineWidth = 10;

    // 开始绘制
    [bezierPath stroke];
}
iOS <wbr> <wbr>两张图片合并成一张。
屏幕快照 2015-12-30 16.32.44.png


版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/ochenmengo/article/details/82880801
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-06-28 01:58:03
  • 阅读 ( 1528 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢