2011-12-13 71 views
0

我有兩個圈,一個大一個小。我想從較大的一個剪下較小的圓,然後使用新的形狀(帶有大洞的大圓圈)將其應用於任意圖像。我玩過一點石英,但找不到解決辦法。有沒有簡單的方法來做到這一點?iPhone如何剪切圈內的圓?

回答

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