图片圆角处理封装

来源:互联网 发布:myflow.js 源码介绍 编辑:程序博客网 时间:2024/06/07 03:20

图片圆角处理封装有两种方式:

1:在Layer层上做处理。(缺点:一个页面出现多个需要裁剪的图片时,程序会很卡顿)

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. self.profileImageView.layer.cornerRadius = self.profileImageView.width * 0.5;  
  2.     self.profileImageView.clipsToBounds = YES;  

2:对UIImage进行封装处理。(调用方便,不会造成程序的卡顿)

创建UIImage的扩展类。

UIImage+LMExtension.h文件中

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface UIImage (LMExtension)  
  4. /** 返回圆形图片*/  
  5. -(UIImage *)circleImage;  
  6. @end  

UIImage+LMExtension.m文件中

[objc] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. //  Created by limin on 16/7/5.  
  2. //  Copyright © 2016年 limin. All rights reserved.  
  3. //cocos2d 。开启图形上下文(透明)  
  4.   
  5. #import "UIImage+LMExtension.h"  
  6.   
  7. @implementation UIImage (LMExtension)  
  8. /** 返回圆形图片*/  
  9. -(UIImage *)circleImage  
  10. {  
  11.     //NO:透明  
  12.     UIGraphicsBeginImageContextWithOptions(self.sizeNO0.0);  
  13.     //获得上下文  
  14.     CGContextRef ctx = UIGraphicsGetCurrentContext();  
  15.     //添加一个圆  
  16.     CGRect rect = CGRectMake(00self.size.widthself.size.height);  
  17.     CGContextAddEllipseInRect(ctx, rect);  
  18.     //裁剪  
  19.     CGContextClip(ctx);  
  20.     //将图片画上去  
  21.     [self drawInRect:rect];  
  22.       
  23.     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();  
  24.       
  25.     UIGraphicsEndImageContext();  
  26.       
  27.     return image;  
  28. }  
  29. @end  
0 0
原创粉丝点击