iOS 设置图片imageView圆角——对图片进行裁剪
Posted 刚刚在线
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS 设置图片imageView圆角——对图片进行裁剪相关的知识,希望对你有一定的参考价值。
以前设置图片圆角总是把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
调用方法:
1 UIImage * image = [UIImageimageNamed:@"123.jpg"]; // 设置原图 2 3 CGSize size = CGSizeMake(100,100); // 设置尺寸 4 5 UIImageView *testImageView = [[UIImageView alloc] init]; 6 7 testImageView.frame = CGRectMake(30, 30, imageWidth, imageWidth); 8 9 testImageView.backgroundColor = [UIColor lightGrayColor]; 10 11 testImageView.contentMode = UIViewContentModeScaleAspectFit; 12 13 [self.view addSubview:testImageView]; 14 15 testImageView.image = [UIImagecreateRoundedRectImage:image size:size radius:10]; // 设置radius
其实github上有个提供对image多种处理的库:
UIImage+Resize 调整图片大小
GitHub:https://github.com/coryalder/UIImage_Resize
提供多种方法为图片设置透明度、圆角、裁剪、调整大小等:
1 - (UIImage *)imageWithAlpha; 2 - (UIImage *)transparentBorderImage:(NSUInteger)borderSize; 3 - (UIImage *)roundedCornerImage:(NSInteger)cornerSize borderSize:(NSInteger)borderSize; 4 - (UIImage *)croppedImage:(CGRect)bounds; 5 - (UIImage *)thumbnailImage:(NSInteger)thumbnailSize 6 transparentBorder:(NSUInteger)borderSize 7 cornerRadius:(NSUInteger)cornerRadius 8 interpolationQuality:(CGInterpolationQuality)quality; 9 - (UIImage *)resizedImage:(CGSize)newSize 10 interpolationQuality:(CGInterpolationQuality)quality; 11 - (UIImage *) 12 resizedImageWithContentMode:(UIViewContentMode)contentMode 13 bounds:(CGSize)bounds 14 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
2. http://www.cnblogs.com/A--G/p/4779759.html
以上是关于iOS 设置图片imageView圆角——对图片进行裁剪的主要内容,如果未能解决你的问题,请参考以下文章
Android高性能的圆角图片控件RoundImageView