English 中文(简体)
Mask white color out of PNG image on iPhone
原标题:

I know that there are already 2 relevant posts on that, but it s not clear...

So the case is this...:

I have a UIImage which consists of a png file requested from a url on the net. Is it possible to mask out the white color so it becomes transparent?

Everything I have tried so far with CGImageCreateWithMaskingColors, returns a white image...

Any help guys would be precious :)

最佳回答

Ok since I found a solution and it s working fine, I am answering my question and I really believe that this will be useful to many people having the same need.

First of all I thought that would be nice to extend my UIImages via a category:

UIImage+Utils.h

#import <UIKit/UIKit.h>

@interface UIImage (Utils)

- (UIImage*)imageByClearingWhitePixels;
@end

UIImage+Utils.m

#import "UIImage+Utils.h"

@implementation UIImage (Utils)

#pragma mark Private Methods
- (CGImageRef) CopyImageAndAddAlphaChannel:(CGImageRef) sourceImage {
    CGImageRef retVal = NULL;

    size_t width = CGImageGetWidth(sourceImage);
    size_t height = CGImageGetHeight(sourceImage);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef offscreenContext = CGBitmapContextCreate(NULL, width, height, 8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);

    if (offscreenContext != NULL) {
        CGContextDrawImage(offscreenContext, CGRectMake(0, 0, width, height), sourceImage);

        retVal = CGBitmapContextCreateImage(offscreenContext);
        CGContextRelease(offscreenContext);
    }

    CGColorSpaceRelease(colorSpace);

    return retVal;
}

- (UIImage *) getMaskedArtworkFromPicture:(UIImage *)image withMask:(UIImage *)mask{

    UIImage *maskedImage;
    CGImageRef imageRef = [self CopyImageAndAddAlphaChannel:image.CGImage];
    CGImageRef maskRef = mask.CGImage;
    CGImageRef maskToApply = CGImageMaskCreate(CGImageGetWidth(maskRef),CGImageGetHeight(maskRef),CGImageGetBitsPerComponent(maskRef),CGImageGetBitsPerPixel(maskRef),CGImageGetBytesPerRow(maskRef),CGImageGetDataProvider(maskRef), NULL, NO);
    CGImageRef masked = CGImageCreateWithMask(imageRef, maskToApply);
    maskedImage = [UIImage imageWithCGImage:masked];
    CGImageRelease(imageRef);
    CGImageRelease(maskToApply);
    CGImageRelease(masked);
    return maskedImage;

}

#pragma mark Public Methods
- (UIImage*)imageByClearingWhitePixels{

    //Copy image bitmaps
    float originalWidth = self.size.width;
    float originalHeight = self.size.height;
    CGSize newSize;

    newSize = CGSizeMake(originalWidth, originalHeight);

    UIGraphicsBeginImageContext( newSize );

    [self drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    //Clear white color by masking with self
    newImage = [self getMaskedArtworkFromPicture:newImage withMask:newImage];

    return newImage;
}

@end

Finally, I am able to use it like this: (After I have imported UIImage+Utils.h of course)

UIImage *myImage = [...]; //A local png file or a file from a url
UIImage *myWhitelessImage = [myImage imageByClearingWhitePixels]; // Hooray!
问题回答

It is not really possible, at least no easy way, certainly no API call. It is basically an image editing problem usually done with Photoshop. Most images that have a transparent background were created that way.





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签