iOS陆哥开发笔记(十九) (Quartz2D图片剪切)
2016-04-14 17:59
465 查看
1.把图片显示在自定义的view中
先把图片绘制到view上。按照原始大小,把图片绘制到一个点上。
代码:
- (void)drawRect:(CGRect)rect
{
UIImage *image2 = [UIImage imageNamed:@"me"];
[image2 drawAtPoint:CGPointMake(100, 100)];
}
2.剪切图片让图片圆形展示
思路:先画一个圆,让图片显示在圆的内部,超出的部分不显示。
注意:显示的范围只限于指定的剪切范围,无论往上下文中绘制什么东西,只要超出了这个范围的都不会显示。
代码:
- (void)drawRect:(CGRect)rect
{
//画圆,以便以后指定可以显示图片的范围
//获取图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(ctx, CGRectMake(100, 100, 50, 50));
//指定上下文中可以显示内容的范围就是圆的范围
CGContextClip(ctx);
UIImage *image2=[UIImage imageNamed:@"me"];
[image2 drawAtPoint:CGPointMake(100, 100)];
}
3.剪切图片让图片三角形展示
代码:
- (void)drawRect:(CGRect)rect
{
//画三角形,以便以后指定可以显示图片的范围
//获取图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//
CGContextAddEllipseInRect(ctx, CGRectMake(100, 100, 50, 50));
CGContextMoveToPoint(ctx, 100, 100);
CGContextAddLineToPoint(ctx, 60, 150);
CGContextAddLineToPoint(ctx, 140, 150);
CGContextClosePath(ctx);
//注意:指定范围(也就是指定剪切的方法一定要在绘制范围之前进行调用)
//指定上下文中可以显示内容的范围就是圆的范围
CGContextClip(ctx);
UIImage *image2=[UIImage imageNamed:@"me"];
[image2 drawAtPoint:CGPointMake(100, 100)];
}
先把图片绘制到view上。按照原始大小,把图片绘制到一个点上。
代码:
- (void)drawRect:(CGRect)rect
{
UIImage *image2 = [UIImage imageNamed:@"me"];
[image2 drawAtPoint:CGPointMake(100, 100)];
}
2.剪切图片让图片圆形展示
思路:先画一个圆,让图片显示在圆的内部,超出的部分不显示。
注意:显示的范围只限于指定的剪切范围,无论往上下文中绘制什么东西,只要超出了这个范围的都不会显示。
代码:
- (void)drawRect:(CGRect)rect
{
//画圆,以便以后指定可以显示图片的范围
//获取图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(ctx, CGRectMake(100, 100, 50, 50));
//指定上下文中可以显示内容的范围就是圆的范围
CGContextClip(ctx);
UIImage *image2=[UIImage imageNamed:@"me"];
[image2 drawAtPoint:CGPointMake(100, 100)];
}
3.剪切图片让图片三角形展示
代码:
- (void)drawRect:(CGRect)rect
{
//画三角形,以便以后指定可以显示图片的范围
//获取图形上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//
CGContextAddEllipseInRect(ctx, CGRectMake(100, 100, 50, 50));
CGContextMoveToPoint(ctx, 100, 100);
CGContextAddLineToPoint(ctx, 60, 150);
CGContextAddLineToPoint(ctx, 140, 150);
CGContextClosePath(ctx);
//注意:指定范围(也就是指定剪切的方法一定要在绘制范围之前进行调用)
//指定上下文中可以显示内容的范围就是圆的范围
CGContextClip(ctx);
UIImage *image2=[UIImage imageNamed:@"me"];
[image2 drawAtPoint:CGPointMake(100, 100)];
}
相关文章推荐
- iOS开发中的小Tips
- ios Method Swizzling学习笔记
- iOS陆哥开发笔记(十八) (Quartz2D矩阵操作)
- iOS陆哥开发笔记(十六) (Quartz2D简单使用二)
- iOS陆哥开发笔记(十五) (Quartz2D简单使用一)
- iOS陆哥开发笔记(十四) (Quartz2D简单介绍)
- iOS陆哥开发笔记(十三) (Xcode统计代码行数)
- iOS开发小技巧--高斯模糊框架的应用
- nagios云监控
- Xamarin.iOS - Handing Keyboard
- iOS陆哥开发笔记(十二) (tableViewCell侧滑显示多个按钮)
- iOS开发之MBProgressHUD的使用
- iOS 项目审核悲剧原因
- iOS陆哥开发笔记(十一) (容易造成循环引用的三种场景)
- iOS陆哥开发笔记(十) (iOS设计模式—单例模式)
- ios7之后的动画
- iOS开发小技巧--键盘处理以及解决block造成循环引用的小技巧
- iOS陆哥开发笔记(九) (SDWebImage简单使用)
- iOS - 加速计(重力感应)
- iOS陆哥开发笔记(八) (GCD死锁及解决方案)