본문 바로가기

Programming/iOS

UIImage 특정 색상을 투명처리

반응형

UIImage 특정 색상을 투명처리하고 싶을 때 사용하는 방법이다

- (UIImage *)changeWhiteColorTransparent:(UIImage *)image {
    CGImageRef rawImageRef= image.CGImage;
    
    const float colorMasking[6] = {222, 255, 222, 255, 222, 255};    // 흰색
//  const float colorMasking[6] = {0, 0, 0, 0, 0, 0};    // 검정색

    UIGraphicsBeginImageContext(image.size);
    CGImageRef maskedImageRef = CGImageCreateWithMaskingColors(rawImageRef, colorMasking);

    {
        //if in iphone
        CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0, image.size.height);
        CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0);
    }

    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height), maskedImageRef);
    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
    CGImageRelease(maskedImageRef);
    UIGraphicsEndImageContext();

    return result;
}

출처

http://stackoverflow.com/questions/633722/how-to-make-one-color-transparent-on-a-uiimage

반응형