开发笔记 那些年追过的图片(十) 图片处理成圆形
2013-10-12 09:33
633 查看
[plain] view
plaincopy
#import <UIKit/UIKit.h>
@interface UIImage (Rounded)
+ (id)createRoundedRectImage:(UIImage*)image size:(CGSize)size radius:(NSInteger)r;
@end
[plain] view
plaincopy
#import "UIImage+Rounded.h"
@implementation UIImage (Rounded)
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;
}
plaincopy
#import <UIKit/UIKit.h>
@interface UIImage (Rounded)
+ (id)createRoundedRectImage:(UIImage*)image size:(CGSize)size radius:(NSInteger)r;
@end
[plain] view
plaincopy
#import "UIImage+Rounded.h"
@implementation UIImage (Rounded)
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;
}
相关文章推荐
- 开发笔记 那些年追过的图片(四):读取图片的信息
- 开发笔记 那些年追过的图片(七):屏幕截图
- 开发笔记 那些年追过的图片(四):读取图片的信息
- 开发笔记 那些年追过的图片(九)图片转PDF的实现方法
- 开发笔记 那些年追过的图片(三):自定义修改拍照界面retake和use按钮
- 开发笔记 那些年追过的图片(三):自定义修改拍照界面retake和use按钮
- 开发笔记 那些年追过的图片(四):改变相册里的英文变成中文
- 开发笔记 那些年追过的图片(五):imageView GIF 动画
- 开发笔记 那些年追过的图片(二): 读取相册里的图片 和 拍摄获取照片
- iOS开发笔记-将图片处理成圆形或者说特定圆角
- 开发笔记 那些年追过的图片(八):PNG,JPEG格式相互转换
- 开发笔记 那些年追过的图片(五):imageView GIF 动画
- 开发笔记 那些年追过的图片(一): image的读取
- 开发笔记 那些年追过的图片(二): 读取相册里的图片 和 拍摄获取照片
- 开发笔记 那些年追过的图片(七):屏幕截图
- 开发笔记 那些年追过的图片(十一)给图片加文字,加水印等
- 开发笔记 那些年追过的图片(六):压缩图片并保存到沙盒里
- IOS开发笔记 - 基于SDWebImage的网络图片加载处理
- IOS开发笔记 - 基于SDWebImage的网络图片加载处理
- IOS开发笔记 - 基于SDWebImage的网络图片加载处理