2011-12-13 2 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; 
} 
관련 문제