这是iPhone 3.0及更高版本中提供的更简单的方法.每个基于视图的对象都有一个关联的图层.每个图层都可以设置一个角半径,这将为您提供所需的内容:
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]];
我要继续这里,实际上回答标题中的问题.
试试这个类别.
的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
如果appIconImage是UIImageView,那么:
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];
还要记住:
#import