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

调用方法:
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
iOS 设置图片imageView圆角——对图片进行裁剪的更多相关文章
- iOS | Swift图片剪切圆角
我们在IOS开发中,时常需要将一些原本是方形的图片剪切成圆形或者边框是曲线的样子,表现得活泼生动一些. 注意:因为最近在尝试用Swiftl开发,所以这里的语言使用的就是Swift,OC的语法也是相近的 ...
- iOS设置图片名称、启动图片、防止TabBar图片和文字渲染
设置App的名称 设置App的启动图片 需要注意点是,App要杀掉重启才能显示出启动图片 2种方法防止图片被渲染 1. vc02.tabBarItem.image = [UIImage imageNa ...
- IOS 将图片转换为圆角图
UIImage+wiRoundedRectImage.h #import <UIKit/UIKit.h> @interface UIImage (wiRoundedRectImage) + ...
- IOS设置图片背景
在UIViewController里面这样设置: self.view.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageN ...
- iOS 设置控件圆角、文字、字体
以按钮为例: UIButton btn = [UIButton new]; btn.layer.masksToBounds = YES; btn.layer.cornerRadius = 10.0; ...
- iOS 开发--开源图片处理圆角
概述 开源项目名称:HYBImageCliped 当前版本:2.0.0 项目用途:可给任意继承UIView的控件添加任意多个圆角.可根据颜色生成图片且可带任意个圆角.给UIButton设置不同状态下的 ...
- android ImageView网络图片加载、动态设置尺寸、圆角..
封装了一个关于ImageView的辅助类,该类可以方便实现网络图片下载的同时,动态设置图片尺寸.圆角.....一系列连贯的操作,无样式表,java代码实现所有功能,使用很方便. package com ...
- iOS圆形图片裁剪,以及原型图片外面加一个圆环
废话不多说,直接上代码 #import "ViewController.h" @interface ViewController () @property (nonatomic,s ...
- IOS 设置圆角用户头像
在App中有一个常见的功能,从系统相册或者打开照相机得到一张图片,然后作为用户的头像.从相册中选取的图片明明都是矩形的图片,但是展示到界面上却变成圆形图片,这个神奇的效果是如何实现的呢? 请大家跟着下 ...
随机推荐
- [CODEVS1048]石子归并
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 有n堆石子排成一列,每堆石子有一个重量w[i], 每次合并可以合并相邻的两堆石子 ...
- Linear Regreesion
3.似然函数:我是这么理解的,比如说我们知道某个X的概率分布密度函数,但是这个概率分布有未知的参数,但是我想得到这个未知的参数θ,然后我们就通过很多个已知的变量,把这些概率分布密度函数乘起来,这 ...
- HTTP 和 SOAP 标头 来传递用户名密码 验证webservice用户认证
支持自定义的 HTTP 和 SOAP 标头 注意:本主题中的内容适用于 Microsoft Office SharePoint Server 2007 SP1. 对于 Web 服务,您可以使用 HTT ...
- PHP面向对象编程
IBM 教程:开始了解 PHP 中的对象 简明现代魔法 一篇很好的入门的Class 文章 - 技术分享 - php教程 少走弯路去学习面向对象编程 -- 简明现代魔法
- A Tour of Go Struct Fields
Struct fields are accessed using a dot. package main import "fmt" type Vertex struct { X i ...
- hdoj 1242 Rescue
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- 征服 Redis + Jedis + Spring (一)—— 配置&常规操作(GET SET DEL)
有日子没写博客了,真的是忙得要疯掉. 完成项目基础架构搭建工作,解决了核心技术问题,接着需要快速的调研下基于Spring框架下的Redis操作. 相关链接: 征服 Redis 征服 Redis + J ...
- MP3的频率、比特率、码率与音质的关系
想知道MP3的频率.比特率.码率与音质的关系,是不是频率越高,码率越高,音质就越好.好像MP3大多数的频率都是44100HZ的.码率有128,192等等. 这里所说的频率是採样率,一般都是44100K ...
- oracle并行模式(Parallel)
1. 用途 强行启用并行度来执行当前SQL.这个在Oracle 9i之后的版本可以使用,之前的版本现在没有环境进行测试.也就是说,加上这个说明,可以强行启用Oracle的多线程处理功能.举例的话,就 ...
- 【转】三次握手与accept()函数
1. 客户端发送SYN给服务器 2. 服务器发送SYN+ACK给客户端 3. 客户端发送ACK给服务器 4. 连接建立,调用accept()函数获取连接