如何在iOS中将图像裁剪成多边形?

时间:2012-09-05 11:36:43

标签: objective-c ios

我想将UIImageview上的图像裁剪成任何形状

2 个答案:

答案 0 :(得分:2)

您设置剪切路径并瞧:

// Load image thumbnail
    NSString *imageName = [self.picArray objectAtIndex:indexPath.row];
    UIImage *image = [UIImage imageNamed:imageName];        
    CGSize imageSize = image.size;
    CGRect imageRect = CGRectMake(0, 0, imageSize.width, imageSize.height);    

    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0.0);
    // Create the clipping path and add it
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:imageRect cornerRadius:5.0f];
    [path addClip];
    [image drawInRect:imageRect];
    UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

此代码加载图像并通过舍入矩形来创建路径,结果是最终图像已被剪切,即圆角。 RoundedImage就是结果。

答案 1 :(得分:0)

您可以使用CGImageMask

样本存在于Apple的QuartzDemo的QuartzMaskingView类中。

相关问题