Rounded Corners on UIImage

前端 未结 12 1058
终归单人心
终归单人心 2020-12-02 04:08

I\'m trying to draw images on the iPhone using with rounded corners, a la the contact images in the Contacts app. I\'ve got code that generally work, but it occasionally cra

12条回答
  •  鱼传尺愫
    2020-12-02 04:34

    I'm gonna go ahead here and actually answer the question in the title.

    Try this category.

    UIImage+additions.h

    #import 
    
    @interface UIImage (additions)
    -(UIImage*)makeRoundCornersWithRadius:(const CGFloat)RADIUS;
    @end
    

    UIImage+additions.m

    #import "UIImage+additions.h"
    
    @implementation UIImage (additions)
    -(UIImage*)makeRoundCornersWithRadius:(const CGFloat)RADIUS {
        UIImage *image = self;
    
        // Begin a new image that will be the new image with the rounded corners
        // (here with the size of an UIImageView)
        UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale);
    
        const CGRect RECT = CGRectMake(0, 0, image.size.width, image.size.height);
        // Add a clip before drawing anything, in the shape of an rounded rect
        [[UIBezierPath bezierPathWithRoundedRect:RECT cornerRadius:RADIUS] addClip];
        // Draw your image
        [image drawInRect:RECT];
    
        // Get the image, here setting the UIImageView image
        //imageView.image
        UIImage* imageNew = UIGraphicsGetImageFromCurrentImageContext();
    
        // Lets forget about that we were drawing
        UIGraphicsEndImageContext();
    
        return imageNew;
    }
    @end
    

提交回复
热议问题