简体   繁体   中英

iPhone How to clip circle inside a circle?

I have two circles, one bigger and one smaller. I want to clip smaller circle from bigger one, and then use that new shape (big circle with a hole in it) to apply it to arbitrary image. I have played a little bit with quartz, but could not find solution to this. Is there any easy way to do this?

here's some code I got from stackoverflow. You would call it once to create your image with a hole mask, then call it again to use that image to mask your source image.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM