美文网首页
iOS 图片切到圆角图片

iOS 图片切到圆角图片

作者: woo_5857 | 来源:发表于2021-09-12 11:51 被阅读0次

    图片切到圆角图片

    /**

    * @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];

    }

    相关文章

      网友评论

          本文标题:iOS 图片切到圆角图片

          本文链接:https://www.haomeiwen.com/subject/kqffgltx.html