iOS開發UI篇—Quartz2D使用(圖片剪切)

 

1、使用Quartz2D完成圖片剪切
1.把圖片顯示在自定義的view中
先把圖片繪製到view上。按照原始大小,把圖片繪製到一個點上。
代碼:
 
1 - (void)drawRect:(CGRect)rect
2 {
3     UIImage *image2=[UIImage imageNamed:@"me"];
4     [image2 drawAtPoint:CGPointMake(100, 100)];
5 }

顯示:spa

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)];
}code

複製代碼
顯示:
 
3.剪切圖片讓圖片三角形展現
代碼:
複製代碼

- (void)drawRect:(CGRect)rect
{blog

//畫三角形,以便之後指定能夠顯示圖片的範圍
//獲取圖形上下文
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)];
}圖片

 
 
複製代碼

顯示:ip

    

相關文章
相關標籤/搜索