以前设置图片圆角总是把imageView设置成圆形,然后设置maskToBounds为YES,其实这样处理很消耗性能,图片多了之后比较卡,最好将图片进行裁剪后显示;这里有个分类可以用:
UIImage+wiRoundedRectImage.h
#import <UIKit/UIKit.h> @interface UIImage (wiRoundedRectImage) + (id)createRoundedRectImage:(UIImage*)image size:(CGSize)size radius:(NSInteger)r; @end
UIImage+wiRoundedRectImage.m
#import "UIImage+wiRoundedRectImage.h" @implementation UIImage (wiRoundedRectImage) static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth,
float ovalHeight)
{
float fw, fh; if (ovalWidth == 0 || ovalHeight == 0)
{
CGContextAddRect(context, rect);
return;
} CGContextSaveGState(context);
CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect));
CGContextScaleCTM(context, ovalWidth, ovalHeight);
fw = CGRectGetWidth(rect) / ovalWidth;
fh = CGRectGetHeight(rect) / ovalHeight; CGContextMoveToPoint(context, fw, fh/2); // Start at lower right corner
CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1); // Top right corner
CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); // Top left corner
CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); // Lower left corner
CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // Back to lower right CGContextClosePath(context);
CGContextRestoreGState(context);
} + (id)createRoundedRectImage:(UIImage*)image size:(CGSize)size radius:(NSInteger)r
{
// the size of CGContextRef
int w = size.width;
int h = size.height; UIImage *img = image;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGRect rect = CGRectMake(0, 0, w, h); CGContextBeginPath(context);
addRoundedRectToPath(context, rect, r, r);
CGContextClosePath(context);
CGContextClip(context);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
img = [UIImage imageWithCGImage:imageMasked]; CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
CGImageRelease(imageMasked); return img;
} @end
调用方法:
UIImage * image = [UIImageimageNamed:@"123.jpg"]; // 设置原图 CGSize size = CGSizeMake(,); // 设置尺寸 UIImageView *testImageView = [[UIImageView alloc] init]; testImageView.frame = CGRectMake(, , imageWidth, imageWidth); testImageView.backgroundColor = [UIColor lightGrayColor]; testImageView.contentMode = UIViewContentModeScaleAspectFit; [self.view addSubview:testImageView]; testImageView.image = [UIImagecreateRoundedRectImage:image size:size radius:]; // 设置radius
其实github上有个提供对image多种处理的库:
UIImage+Resize 调整图片大小
GitHub:https://github.com/coryalder/UIImage_Resize
提供多种方法为图片设置透明度、圆角、裁剪、调整大小等:
- (UIImage *)imageWithAlpha;
- (UIImage *)transparentBorderImage:(NSUInteger)borderSize;
- (UIImage *)roundedCornerImage:(NSInteger)cornerSize borderSize:(NSInteger)borderSize;
- (UIImage *)croppedImage:(CGRect)bounds;
- (UIImage *)thumbnailImage:(NSInteger)thumbnailSize
transparentBorder:(NSUInteger)borderSize
cornerRadius:(NSUInteger)cornerRadius
interpolationQuality:(CGInterpolationQuality)quality;
- (UIImage *)resizedImage:(CGSize)newSize
interpolationQuality:(CGInterpolationQuality)quality;
- (UIImage *)
resizedImageWithContentMode:(UIViewContentMode)contentMode
bounds:(CGSize)bounds
interpolationQuality:(CGInterpolationQuality)quality;
更详细使用见:http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
参考链接:1. http://www.cnblogs.com/thefeelingofsimple/archive/2013/02/20/2918547.html