Rounded Corners on UIImage

前端 未结 12 977
终归单人心
终归单人心 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:30

    Here is an even easier method that is available in iPhone 3.0 and up. Every View-based object has an associated layer. Each layer can have a corner radius set, this will give you just what you want:

    UIImageView * roundedView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:@"wood.jpg"]];
    // Get the Layer of any view
    CALayer * l = [roundedView layer];
    [l setMasksToBounds:YES];
    [l setCornerRadius:10.0];
    
    // You can even add a border
    [l setBorderWidth:4.0];
    [l setBorderColor:[[UIColor blueColor] CGColor]];
    
    0 讨论(0)
  • 2020-12-02 04:32

    If you are calling your method (borderedImageWithRect) in a background thread, crashes might occur since UIGraphics-functions are not thread-safe. In such a case, you must create a context using CGBitmapContextCreate() - see the "Reflection" sample code from the SDK.

    0 讨论(0)
  • 2020-12-02 04:33

    I actually had a chance to talk about this with somebody from Apple at the iPhone Tech Talk in New York. When we talked about it, he was pretty sure it wasn't a threading issued. Instead, he thought that I needed to retain the graphics context that was generated when calling UIGraphicsBeginImageContext. This seems to violate the general rules dealing with retain rules and naming schemes, but this fellow was pretty sure he'd seen the issue previously.

    If the memory was getting scribbled, perhaps by another thread, that would certainly explain why I was only seeing the issue occasionally.

    I haven't had time to revisit the code and test out the fix, but PCheese's comment made me realize I hadn't posted the info here.

    ...unless I wrote that down wrong and UIGraphicsBeginImageContext should've been CGBitmapContextCreate...

    0 讨论(0)
  • 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 <UIKit/UIKit.h>
    
    @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
    
    0 讨论(0)
  • 2020-12-02 04:38

    I would reiterate fjoachim's answer: be cautious when attempting to draw while running on a separate thread, or you may get EXC_BAD_ACCESS errors.

    My workaround went something like this:

    UIImage *originalImage = [UIImage imageNamed:@"OriginalImage.png"] 
    [self performSelectorOnMainThread:@selector(displayImageWithRoundedCorners:) withObject:originalImage waitUntilDone:YES];
    

    (In my case I was resizing / scaling UIImages.)

    0 讨论(0)
  • 2020-12-02 04:42

    If appIconImage is an UIImageView, then:

    appIconImage.image = [UIImage imageWithContentsOfFile:@"image.png"]; 
    appIconImage.layer.masksToBounds = YES;
    appIconImage.layer.cornerRadius = 10.0;
    appIconImage.layer.borderWidth = 1.0;
    appIconImage.layer.borderColor = [[UIColor grayColor] CGColor];
    

    And also remember:

    #import <QuartzCore/QuartzCore.h>
    
    0 讨论(0)
提交回复
热议问题