把一个图片裁剪成圆形或者自定义的图形
2016-08-08 12:18
330 查看
- (UIImage *)circleImage
{
// NO代表透明
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
// 获得上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 添加一个圆
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextAddEllipseInRect(ctx, rect);
// 裁剪
CGContextClip(ctx);
// 将图片画上去
[self drawInRect:rect];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (instancetype)setCustomShapeImage{
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(ctx, 0, 0);
CGContextAddLineToPoint(ctx, 387, 387);
CGContextAddLineToPoint(ctx, 0, 387);
CGContextAddLineToPoint(ctx, 0, 0);
CGContextClip(ctx);
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
{
// NO代表透明
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
// 获得上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
// 添加一个圆
CGRect rect = CGRectMake(0, 0, self.size.width, self.size.height);
CGContextAddEllipseInRect(ctx, rect);
// 裁剪
CGContextClip(ctx);
// 将图片画上去
[self drawInRect:rect];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
- (instancetype)setCustomShapeImage{
UIGraphicsBeginImageContextWithOptions(self.size, NO, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(ctx, 0, 0);
CGContextAddLineToPoint(ctx, 387, 387);
CGContextAddLineToPoint(ctx, 0, 387);
CGContextAddLineToPoint(ctx, 0, 0);
CGContextClip(ctx);
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
相关文章推荐
- 自定义画一个可以拖动的圆形或图片
- 自定义之一个图片根据另一个图片形状进行裁剪
- Android自定义view实现圆形、圆角和椭圆图片(BitmapShader图形渲染)
- 自定义BitmapTransformation完美解决Glide加载圆角或者圆形图片
- 位图上下文 裁剪图片成一个圆形的头像
- Android 使用自定义Drawable 设置圆角矩形或者圆形图片
- 自定义圆形或者圆角矩形图片
- Android 图片圆角,自定义圆角的弧度,或者直接设置为圆形图片
- Android 自定义View修炼-Android实现圆形、圆角和椭圆自定义图片View(使用BitmapShader图形渲染方法)
- Android使用BitmapShader图形渲染实现圆形、圆角和椭圆自定义图片View
- 淘宝灵活的圆角框--通过一个圆形图片形成圆角原理
- 关于自定义View时,画图形和图片时抗锯齿的使用的问题
- 仿一个wp7中PhotoChooserTask指定宽高后的图片裁剪窗口
- 自定义UITabBar的背景图片或者颜色
- 关于自定义View时,画图形和图片时抗锯齿的使用的问题
- android自定义View实现裁剪图片功能,不使用系统的
- 自定义一个简单的带图片的Toast
- 如何写一个像UIImagePicker一样的自定义图片选择器
- 在没有图片的情况下用css3实现一个圆形
- JAVA 如何将一个图形(以文件存在,如JPG或者GIF)画到窗体的画布中