iphone UIbezierpath不规则图像裁剪

问题描述:

我正在为iPhone创建拼图游戏。iphone UIbezierpath不规则图像裁剪

在这里,我要裁剪图像,就像下面的图片

enter image description here

我才知道uibezier路径是在不规则形状裁剪图像的优秀企业之一的谷歌搜索后。

但我没有得到任何代码或想法如何裁剪像上面的图像。

+0

看看我的答案在这里 http://stackoverflow.com/questions/13153223/how-to-crop-the-image-using-uibezierpath/ 15353848#15353848 –

这将需要大量的试验和错误,我很快就做到了这一点,只是为了让您了解如何使用贝塞尔路径创建形状。下面是示例代码来创建我快速创建

UIBezierPath *aPath = [UIBezierPath bezierPath]; 

    // Set the starting point of the shape. 
    [aPath moveToPoint:CGPointMake(50.0, 50.0)]; 
    // Draw the lines. 
    [aPath addLineToPoint:CGPointMake(100.0, 50.0)]; 
    [aPath addLineToPoint:CGPointMake(100.0, 100.0)]; 
    [aPath addLineToPoint:CGPointMake(50.0, 100.0)]; 
    [aPath closePath]; 

    CAShapeLayer *square = [CAShapeLayer layer]; 
    square.path = aPath.CGPath; 
    [self.view.layer addSublayer:square]; 

如果我有更多的时间,我可以创造的形象,但这样做快没有太多的时间为正方形。一旦你让你的脑袋变得圆满如何,它不难用。创建此形状需要大量试验和错误,但使用我提供的代码作为如何使用贝塞尔路径创建形状的基础。您需要创建更多的点才能最终形成您想要的形状。

我也建议在看苹果开发者指南创建不规则形状

http://developer.apple.com/library/ios/#documentation/2ddrawing/conceptual/drawingprintingios/BezierPaths/BezierPaths.html

。在“添加曲线到您的路径”​​特别找了解如何创建曲线,并将其添加到您的形象。你需要的是,为了创建拼图块的形状,你要创建

编辑:

尝试此方法

- (void) setClippingPath:(UIBezierPath *)clippingPath : (UIImageView *)imgView; 
{ 
    if (![[imgView layer] mask]) 
     [[imgView layer] setMask:[CAShapeLayer layer]]; 

    [(CAShapeLayer*) [[imgView layer] mask] setPath:[clippingPath CGPath]]; 
} 

上述方法将采取贝塞尔路径和ImageView的和然后将bezier路径应用于该特定的imageView。它也会做剪辑。我会想要花费大量的尝试和错误来设计恰当的形状,有时候可能很困难并且很难制作复杂的形状。应用此代码

UIBezierPath *aPath = [UIBezierPath bezierPath]; 
    // Set the starting point of the shape. 
    [aPath moveToPoint:CGPointMake(0.0, 0.0)]; 
    // Draw the lines. 
    [aPath addLineToPoint:CGPointMake(50.0, 0.0)]; 
    [aPath addLineToPoint:CGPointMake(50.0, 50.0)]; 
    [aPath addLineToPoint:CGPointMake(0.0, 50.0)]; 
    [aPath closePath]; 

    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bar.png"]]; 
    imgView.frame = CGRectMake(0, 0, 100, 100); 
    [self setClippingPath:aPath :imgView]; 
    [self.view addSubview:imgView]; 

简单的例子就迅速作出方形出的图像的左上角部分。例如,如果你有一个正方形的图像,你可以遍历图像的宽度和高度,使用上面的代码剪切成单独的正方形并单独返回它们。创建拼图块很复杂,但希望这可以帮助

+0

非常感谢您的回复,并花时间回答我的问题,这真的很可观。是的,现在我清楚在哪条路上我应该旅行,在这里我的另一个问题是裁剪图像。你有什么想法吗?使用这个uibezier路径绘制曲线绘制曲线? – nik

+0

我只是想匹配这种模式与uiimage,我只是想用该模式裁剪图像 – nik

+1

看到我的编辑,我包括一个方法,你可以使用它将采取bezier路径和imageView,并为你做剪裁。希望这有助于 – AdamM