2012-12-25 146 views
0

基本上,我正在制作一个有两个图像的视图。图像一个显示在一个直角三角形中,占据视图的左上角,而图像二则占据右下角的直角三角形。iOS UIImageView剪辑/蒙版图像

想象一下对角线的方形切割,每个结果的一半存在不同的图像。

我一直在阅读很多关于口罩的内容,但我不想用其他图片来掩盖这些图片。

我正在寻找一种方法给它3点,形成那个三角形,然后让它以这种方式剪裁图像。

我觉得这可能很容易在Coregraphics中完成,我只是想念我所想的电话。

任何帮助,非常感谢!

回答

2

下面是一种在图像上下文中使用剪切路径的方法。该示例适用于256 x 256的图像大小,但您应该可以轻松地将其适应您的需求。

UIGraphicsBeginImageContext(CGSizeMake(256, 256)); 
CGContextRef context = UIGraphicsGetCurrentContext(); 
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, 256); 
CGContextConcatCTM(context, flipVertical); 
CGContextBeginPath(context); 
CGContextMoveToPoint(context, 0, 0); 
CGContextAddLineToPoint(context, 0, 256); 
CGContextAddLineToPoint(context, 256, 256); 
CGContextClosePath(context); 
CGContextSaveGState(context); 
CGContextClip(context); 
CGContextDrawImage(context, CGRectMake(0, 0, 256, 256), [image1 CGImage]); 
CGContextRestoreGState(context); 
CGContextBeginPath(context); 
CGContextMoveToPoint(context, 0, 0); 
CGContextAddLineToPoint(context, 256, 0); 
CGContextAddLineToPoint(context, 256, 256); 
CGContextClosePath(context); 
CGContextSaveGState(context); 
CGContextClip(context); 
CGContextDrawImage(context, CGRectMake(0, 0, 256, 256), [image2 CGImage]); 
CGContextRestoreGState(context); 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); // image contains the result 
UIGraphicsEndImageContext();