以前设置图片圆角总是把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

     2. http://www.cnblogs.com/A--G/p/4779759.html

最新文章

  1. C#冒泡排序算法
  2. 复选框css
  3. HTML5 图片缩放功能
  4. IOS系列swift语言之课时四
  5. SQL简单语句总结习题
  6. Centos5.8下编译安装PHP5.4和memcached, phalcon, yaf, apc
  7. win10中文简体繁体切换快捷键
  8. 为什么要在block用weak self
  9. SpatiaLite 各版本数据库差异
  10. js 格式化数字
  11. select、pselect、poll和epoll的区别
  12. JavaScripts学习日记——BOM
  13. Python3基础 setdefault() 根据键查找值,找不到键会添加
  14. ASP日期格式化函数
  15. DocNan博文目录
  16. PAT 1116 Come on! Let&#39;s C
  17. BatchPreparedStatementSetter,用法
  18. Docker部署Jenkins测试环境
  19. VB Byte数组转字符串问题
  20. pom配置详解

热门文章

  1. 支持度(support)和置信度(confidence)
  2. codeforce 651B Beautiful Paintings
  3. Java 并发之共享对象
  4. Grails教程之--我的理解
  5. fckeditor的用法
  6. 使用sessionStorage得一个坑
  7. iOS中事件的传递和响应者链条
  8. 【转】ST05
  9. [转]非常好的vsftpd安装于配置
  10. 礼仪或许就是尊重的还有一个说法——leo鉴书61