如何基于不规则形状裁剪图像?

8

我想在iOS中根据不规则的形状蒙版裁剪图像。我该怎么做?

1个回答

3

我不确定你想要裁剪,还是想要遮罩图像。这很容易做到,但你最终会发现它适用于某些图像而不适用于其他图像。这是因为您需要在图像中具有适当的Alpha通道。

以下是我使用的代码,我从stackoverflow上获取的。(Problem with transparency when converting UIView to UIImage

CGImageRef CopyImageAndAddAlphaChannel(CGImageRef sourceImage) {
CGImageRef retVal = NULL;

size_t width = CGImageGetWidth(sourceImage);
size_t height = CGImageGetHeight(sourceImage);

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

CGContextRef offscreenContext = CGBitmapContextCreate(NULL, width, height, 
                                                      8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);

if (offscreenContext != NULL) {
    CGContextDrawImage(offscreenContext, CGRectMake(0, 0, width, height), sourceImage);

    retVal = CGBitmapContextCreateImage(offscreenContext);
    CGContextRelease(offscreenContext);
}

CGColorSpaceRelease(colorSpace);

return retVal;
}

- (UIImage*)maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
CGImageRef maskRef = maskImage.CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                    CGImageGetHeight(maskRef),
                                    CGImageGetBitsPerComponent(maskRef),
                                    CGImageGetBitsPerPixel(maskRef),
                                    CGImageGetBytesPerRow(maskRef),
                                    CGImageGetDataProvider(maskRef), NULL, false);

CGImageRef sourceImage = [image CGImage];
CGImageRef imageWithAlpha = sourceImage;
//add alpha channel for images that don't have one (ie GIF, JPEG, etc...)
//this however has a computational cost
// needed to comment out this check. Some images were reporting that they
// had an alpha channel when they didn't! So we always create the channel.
// It isn't expected that the wheelin application will be doing this a lot so 
// the computational cost isn't onerous.
//if (CGImageGetAlphaInfo(sourceImage) == kCGImageAlphaNone) { 
imageWithAlpha = CopyImageAndAddAlphaChannel(sourceImage);
//}

CGImageRef masked = CGImageCreateWithMask(imageWithAlpha, mask);
CGImageRelease(mask);

//release imageWithAlpha if it was created by CopyImageAndAddAlphaChannel
if (sourceImage != imageWithAlpha) {
    CGImageRelease(imageWithAlpha);
}

UIImage* retImage = [UIImage imageWithCGImage:masked];
CGImageRelease(masked);

return retImage;
}

然后你可以这样调用它:

    customImage = [customImage maskImage:customImage withMask:[UIImage imageNamed:@"CircularMask.png"]];

在这种情况下,我正在使用一个圆形蒙版来制作圆形图像。您需要制作适合您需求的不规则蒙版。

谢谢您的回复,但我的问题是另外一个。我有四个不同的点,就像一个梯形一样。用户还可以更改这些点并使形状成为自己的风格。现在我想裁剪图像,就像用户通过拖动点创建的相同形状一样。 - Shinning River
你能将这4个点渲染成掩码,然后使用上述代码吗? - Paul Cezanne
抱歉,我是IOS开发的新手。我无法理解您的观点。例如,如果我有梯形的四个点CGPoint firstPoint,secondPoint,thirdPoint和fourthPoint,那么我该如何使用上述代码?谢谢。 - Shinning River
@PaulCezanne:_CopyImageAndAddAlphaChannel未定义符号错误。您能告诉我我的错误是什么吗? - Seeker
你是否复制/粘贴所有的代码?CopyImageAndAddAlphaChannel在我上面的代码示例的第一行中定义。如果你仍需要帮助,请创建一个新问题并将你的源代码放入其中。 - Paul Cezanne
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接