【问题标题】:iPhone How to clip circle inside a circle?iPhone 如何在圆圈内夹圈?
【发布时间】:2011-12-13 11:29:51
【问题描述】:

我有两个圆圈,一个大一个小。我想从较大的圆圈中剪下较小的圆圈,然后使用该新形状(带有孔的大圆圈)将其应用于任意图像。我玩了一点石英,但找不到解决办法。有什么简单的方法可以做到这一点?

【问题讨论】:

    标签: iphone drawing quartz-graphics geometry clip


    【解决方案1】:

    这是我从 stackoverflow 获得的一些代码。您可以调用一次以创建带有孔掩码的图像,然后再次调用它以使用该图像来遮盖您的源图像。

    - (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;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-11-12
      • 2019-07-19
      • 1970-01-01
      • 2021-10-15
      • 1970-01-01
      • 2014-07-13
      • 1970-01-01
      • 1970-01-01
      • 2014-10-13
      相关资源
      最近更新 更多