Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Make UIImage White Background Transparent in iOS

    • 0
    • 3
    • 2
    • 0
    • 4
    • 0
    • 0
    • 0
    • 4.01k
    Comment on it

    If you came across to a requirement where you want to make UIImage Background Transparent then use below method to achieve it.

    +(UIImage *)changeWhiteColorTransparent: (UIImage *)image{
    CGImageRef rawImageRef=image.CGImage;
    
    const CGFloat colorMasking[6] = {222, 255, 222, 255, 222, 255};
    
    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;
    }
    

    It will return you an image with a transparent Background. Happy Coding!!

 4 Comment(s)

  • extension UIImage {

    func imageByMakingWhiteBackgroundTransparent() -> UIImage? {
    
        if let rawImageRef = self.CGImage {
    
            let colorMasking: [CGFloat] = [200, 255, 200, 255, 200, 255]
    
            UIGraphicsBeginImageContext(self.size)
    
            if let maskedImageRef = CGImageCreateWithMaskingColors(rawImageRef, colorMasking) {
    
                CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0, self.size.height)
                CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0)
    
                CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, self.size.width, self.size.height), maskedImageRef)
    
                let result = UIGraphicsGetImageFromCurrentImageContext()
    
                UIGraphicsEndImageContext()
    
                return result
            }
        }
    
        return nil
    }
    

    }

  • A. The code of *CGImageCreateWithMaskingColors* returns nil, probably when trying to handle an image with alpha channel. Can someone know how we can bypass this?

    And for anyone asking - Swift 2.0 code of the extension:

    extension UIImage {

    func imageByMakingWhiteBackgroundTransparent() -> UIImage? {
        if let rawImageRef = self.CGImage {
            //TODO: eliminate alpha channel if exsists
            let colorMasking: [CGFloat] = [200, 255, 200, 255, 200, 255]
            UIGraphicsBeginImageContext(self.size)
            if let maskedImageRef = CGImageCreateWithMaskingColors(rawImageRef, colorMasking) {
                CGContextTranslateCTM(UIGraphicsGetCurrentContext(), 0.0, self.size.height)
                CGContextScaleCTM(UIGraphicsGetCurrentContext(), 1.0, -1.0)
                CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, self.size.width, self.size.height), maskedImageRef)
                let result = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
                return result
            }
        }
        return nil
    }
    

    }

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: