您的位置:首页 > 移动开发 > IOS开发

iOS 利用Context裁剪图片

2014-02-07 14:48 381 查看
下面的代码可以裁剪出圆形的图片,

1,先把不规则图片转成正方形图片

UIGraphicsBeginImageContext(newSize);

[image drawInRect:CGRectMake(0,0,newSize.width,newSize.width)];

UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();


2 ,把正方形图像绘制剪切为圆形

UIGraphicsBeginImageContext(image.size);
CGContextRef context = UIGraphicsGetCurrentContext();

CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
CGContextAddEllipseInRect(context, rect);
CGContextClip(context); //进行剪切,所有context绘制只保留能够被当前path进行fill的区域
[image drawInRect:rect]; //绘制原图

UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: