iOS 设置图片imageView圆角——对图片进行裁剪

摘要:
过去,在设置图像圆角时,图像视图总是设置为圆形,然后maskToBounds设置为YES。事实上,这个过程非常消耗性能。当有许多图像时,这更困难,因此最好裁剪图像并显示它们;以下是可以使用的分类:UIImage+wiRoundedRectImage。h#import @interfaceUIImage(wiRoundedRectImage)+(id)createRoundedRec

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

 

代码Demo:  http://files.cnblogs.com/files/A--G/RoundImageDemo.zip
 

其实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圆角——对图片进行裁剪》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇超越Excel、python的数据可视化报表,月薪3W的人都在用一起谈.NET技术,在 Linux 操作系统中运行 ASP.NET 4 (中) 狼人:下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Spring MVC与Dubbo的整合一

一、Dubbo是什么 一款分布式服务框架 高性能和透明化的RPC远程服务调用方案 SOA服务治理方案 每天为2千多个服务提供大于30亿次访问量支持,并被广泛应用于阿里巴巴集团的各成员站点以及别的公司的业务中。 具体dubbo的背景和简介以及框架等基础知识参考这位大神的博客 二、提供者的Dubbo配置 首先我们先配置服务的提供者 1.给作为提供者的Spr...

《Spring源码深度解析》一

Spring整体架构 1.1 Spring整体架构 1.1.1 Core Container: 模块:Core、Beans、Context和Expression Language Core:框架的基础部分, 提供IOC 和依赖注入特性。也包含核心工具类。基础概念是BeanFactory, 它提供对工厂模式的经典实现来消除对程序性单例模式的需要, 并真正地...

Android Glide加载视频封面

/** *   context 上下文 *   uri 视频地址 *   imageView 设置image *   frameTimeMicros 获取某一时间帧 */ public void loadVideoScreenshot(final Context context, String ur...

Java安全之Weblogic内存马

Java安全之Weblogic内存马 0x00 前言 发现网上大部分大部分weblogic工具都是基于RMI绑定实例回显,但这种方式有个弊端,在Weblogic JNDI树里面能将打入的RMI后门查看得一清二楚。并且这种方式实现上传Webshell落地文件容易被Hids监测。 0x01 调试分析 调试分析 写一个filter进行断点跟踪上层代码。 其实和T...

Android透明无边框圆形进度条之最简单实现

很多人在项目中做长时间操作时,比如访问web service后台数据,都想显示一个透明无边框的圆形进度条,如下图: 不幸的是,Android系统自带的ProgressDialog,无论如何设置Theme、style,或者用java代码设置什么属性,边框都是去不掉的,至少我现在还不知道怎么去掉: 怎么办? 其实很简单,自定义一个ProgressDialo...

iOS 绘制1像素的线

一、Point Vs Pixel iOS中当我们使用Quartz,UIKit,CoreAnimation等框架时,所有的坐标系统采用Point来衡量。系统在实际渲染到设置时会帮助我们处理Point到Pixel的转换。 这样做的好处隔离变化,即我们在布局的事后不需要关注当前设备是否为Retina,直接按照一套坐标系统来布局即可。 实际使用中我们需要...