iOS 图片切到圆角图片

2021-09-12  本文已影响0人  woo_5857

图片切到圆角图片

/**

* @brief clip the cornerRadius with image, UIImageView must be setFrame before, no off-screen-rendered

*/

+ (UIImage *)zy_cornerRadiusWithImage:(UIImage *)image cornerRadius:(CGFloat)cornerRadius rectCornerType:(UIRectCorner)rectCornerType  {

    CGRect bounds = CGRectMake(0, 0, image.size.width, image.size.height);

    CGSize size = image.size;

    CGSize cornerRadii = CGSizeMake(cornerRadius, cornerRadius);

    UIGraphicsBeginImageContextWithOptions(size, NO, 0);

    if (nil == UIGraphicsGetCurrentContext()) {

        return nil;

    }

    UIBezierPath *cornerPath = [UIBezierPath bezierPathWithRoundedRect:bounds byRoundingCorners:rectCornerType cornerRadii:cornerRadii];

    [cornerPath addClip];

    [image drawInRect:bounds];

    //3.从上下文中获取新图片

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;

}

+ (UIImage*)idCardImageCornerRadius:(UIImage *)oldImage {

    return [PretreatmentTool zy_cornerRadiusWithImage:oldImage cornerRadius:100 rectCornerType:UIRectCornerAllCorners];

}

上一篇下一篇

猜你喜欢

热点阅读