开发者

split UIImage by colors and create 2 images

I have looked through replacing colors in an image but cannot get it to work how i need because I am trying to do it with every color but one, as well as transparency.

what I am looking for is a way to take in an image and split out a color (say all the pure black) from that image. Then take that split out portion and make a new image with a transparent background and the split out portion.

(here is j开发者_JAVA百科ust an example of the idea, say i want to take a screenshot of this page. make every other color but pure black be transparent, and save that new image to the library, or put it into a UIImageView)

i have looked in to CGImageCreateWithMaskingColors but cant seem to do what I need with the transparent portion, and I dont really understand the colorMasking input other than you can provide it with a {Rmin,Rmax,Gmin,Gmax,Bmin,Bmax} color mask but when I do, it colors everything. any ideas or input would be great.


Sounds like you're going to have to get access to the underlying bytes and write code to process them directly. You can use CGImageGetDataProvider() to get access to the data of an image, but there's no guarantee that the format will be something you know how to handle. Alternately you can create a new CGContextRef using a specific format you know how to handle, then draw the original image into your new context, then process the underlying data. Here's a quick attempt at doing what you want (uncompiled):

- (UIImage *)imageWithBlackPixels:(UIImage *)image {
    CGImageRef cgImage = image.CGImage;
    // create a premultiplied ARGB context with 32bpp
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    size_t width = CGImageGetWidth(cgImage);
    size_t height = CGImageGetHeight(cgImage);
    size_t bpc = 8; // bits per component
    size_t bpp = bpc * 4 / 8; // bytes per pixel
    size_t bytesPerRow = bpp * width;
    void *data = malloc(bytesPerRow * height);
    CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host;
    CGContextRef ctx = CGBitmapContextCreate(data, width, height, bpc, bytesPerRow, colorspace, bitmapInfo);
    CGColorSpaceRelease(colorspace);
    if (ctx == NULL) {
        // couldn't create the context - double-check the parameters?
        free(data);
        return nil;
    }
    // draw the image into the context
    CGContextDrawImage(ctx, CGRectMake(0, 0, width, height), cgImage);
    // replace all non-black pixels with transparent
    // preserve existing transparency on black pixels
    for (size_t y = 0; y < height; y++) {
        size_t rowStart = bytesPerRow * y;
        for (size_t x = 0; x < width; x++) {
            size_t pixelOffset = rowStart + x*bpp;
            // check the RGB components of the pixel
            if (data[pixelOffset+1] != 0 || data[pixelOffset+2] != 0 || data[pixelOffset+3] != 0) {
                // this pixel contains non-black. zero it out
                memset(&data[pixelOffset], 0, 4);
            }
        }
    }
    // create our new image and release the context data
    CGImageRef newCGImage = CGBitmapContextCreateImage(ctx);
    CGContextRelease(ctx);
    free(data);
    UIImage *newImage = [UIImage imageWithCGImage:newCGImage scale:image.scale orientation:image.imageOrientation];
    CGImageRelease(newCGImage);
    return newImage;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜