【问题标题】:How do I crop an image based on an irregular shape?如何根据不规则形状裁剪图像?
【发布时间】:2012-01-31 21:47:49
【问题描述】:

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

【问题讨论】:

    标签: ios crop


    【解决方案1】:

    我不确定您是否要裁剪,但您想屏蔽图像。这很容易做到,但您最终会发现它适用于某些图像而不适用于其他图像。这是因为您需要在图像中具有正确的 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"]];
    

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

    【讨论】:

    • 感谢您的重播,但我的问题是其他。我有 4 个不同的点,如梯形。用户也可以更改这些点并使形状成为自己的风格。现在我想像相同的形状一样裁剪图像该用户通过拖动点创建
    • 能不能把这4个点,渲染成蒙版,然后用上面的代码?
    • 对不起,我是IOS开发的新手。我无法理解你的意思。例如我有四个点 CGPoint firstPoint , secondPoint,thirdPoint 和 FourthPoint 的梯形然后我如何使用上面的代码谢谢
    • 查看本教程:techotopia.com/index.php/….
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 2012-09-22
    • 1970-01-01
    • 2015-01-28
    • 2012-09-21
    相关资源
    最近更新 更多