如何在UIImage中获取形状的UIBezierPath或以某种形状裁剪UIImage

时间:2014-07-16 01:25:58

标签: ios objective-c uiimage uibezierpath

我是iOS新手,我想知道我是否可以获得UIBezierPath的{​​{1}}。我有一个UIImage的面部布局,想要获得UIImage,这有助于我裁剪UIBezierPath。 或者,任何人都可以告诉我有关裁剪UIImage的其他方法吗?但要确保裁剪处于自定义形状(如:面部,心脏等),而不是矩形。

1 个答案:

答案 0 :(得分:4)

以下是用图像屏蔽图像的代码

- (UIImage *)cerateImageFromImage:(UIImage *)image
                    withMaskImage:(UIImage *)mask {
    CGImageRef imageRef = image.CGImage;
    CGImageRef maskRef = mask.CGImage;

    CGImageRef imageMask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                             CGImageGetHeight(maskRef),
                                             CGImageGetBitsPerComponent(maskRef),
                                             CGImageGetBitsPerPixel(maskRef),
                                             CGImageGetBytesPerRow(maskRef),
                                             CGImageGetDataProvider(maskRef),
                                             NULL,
                                             YES);

    CGImageRef maskedReference = CGImageCreateWithMask(imageRef, imageMask);
    CGImageRelease(imageMask);

    UIImage *maskedImage = [UIImage imageWithCGImage:maskedReference];
    CGImageRelease(maskedReference);

    return maskedImage;
}

<强>用法:

UIImage *image = [UIImage imageNamed:@"Photo.png"];
UIImage *mask = [UIImage imageNamed:@"Mask.png"];
self.imageView.image = [self cerateImageFromImage:image
                                     withMaskImage:mask];