IOS 根据指定图片,画一个带圆角背景的图片

根据指定图片,画一个带圆角背景的图片

/// 这里因为SGQRCodeGenerateManager的方法只需要一张图片,因此这里根据图片创建View,然后把把View投影成图片
/// @param image 被包裹的图片

  • (UIImage *)makeScaledRoundCornerLogo:(UIImage *)image {
    // 1. 设置上下文大小为64 * 屏幕的scale,防止模糊
    CGSize size = CGSizeMake(64 * UIScreen.mainScreen.scale, 64 * UIScreen.mainScreen.scale);
    // - - - - - - - - - - - - - - - - 添加中间小图标 - - - - - - - - - - - - - - - -
    // 2、开启绘图, 获取图形上下文
    UIGraphicsBeginImageContext(CGSizeMake(size.width, size.height));
    // 3、画圆,作出圆角
    UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, size.width, size.height) cornerRadius:10 * UIScreen.mainScreen.scale];
    [path addClip];
    // 3、用白色进行填充
    CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [UIColor.whiteColor CGColor]);
    CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, size.width, size.height));

    // 4、再把小图片画上去
    UIImage *icon_image = image;
    CGFloat icon_imageW = 45 * UIScreen.mainScreen.scale;
    CGFloat icon_imageH = 45 * UIScreen.mainScreen.scale;
    CGFloat icon_imageX = (size.width - icon_imageW) * 0.5;
    CGFloat icon_imageY = (size.height - icon_imageH) * 0.5;

    [icon_image drawInRect:CGRectMake(icon_imageX, icon_imageY, icon_imageW, icon_imageH)];

    // 5、获取当前画得的这张图片
    UIImage *final_image = UIGraphicsGetImageFromCurrentImageContext();

    // 6、关闭图形上下文
    UIGraphicsEndImageContext();

    return final_image;
    }

你可能感兴趣的:(IOS 根据指定图片,画一个带圆角背景的图片)