0

私はobjective-cに少し慣れていないので、Quartz 2Dを使用したプログラミングはさらに新しいので、事前にお詫びします。UIImageから(1つだけではなく)いくつかの特定の色を削除したいメソッドがあります。

カラーマスクを1つだけ適用してプロジェクトを実行すると、美しく機能します。それらをスタックしようとすると、「whiteRef」はNULLになります。メソッドを変更してカラーマスクを取得してから、メソッドを2回実行して(異なるカラーマスクをフィードする)、それでもうまくいきませんでした。

これに関するどんな助けも大歓迎です!

- (UIImage *)doctorTheImage:(UIImage *)originalImage
{
    const float brownsMask[6] = {124, 255, 68, 222, 0, 165};
    const float whiteMask[6] = {255, 255,  255, 255, 255, 255};

    UIImageView *imageView = [[UIImageView alloc] initWithImage:originalImage];

    UIGraphicsBeginImageContext(originalImage.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGImageRef brownRef = CGImageCreateWithMaskingColors(imageView.image.CGImage, brownsMask);    
    CGImageRef whiteRef = CGImageCreateWithMaskingColors(brownRef, whiteMask);    
    CGContextDrawImage (context, CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height), whiteRef);

    CGImageRelease(brownRef);
    CGImageRelease(whiteRef);

    UIImage *doctoredImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [imageView release];

    return doctoredImage;
}
4

1 に答える 1

1

さて、私は良い回避策を見つけました!基本的に、私は最終的にMKMapViewを介して画像データを使用していたので、必要なのは画像をそのピクセルに分解することだけで、そこから私は好きなように混乱することができました。速度の点では最善の選択肢ではないかもしれませんが、うまくいきます。これが私が現在使用しているコードのサンプルです。

//Split the images into pixels to mess with the image pixel colors individually
size_t bufferLength = gridWidth * gridHeight * 4;
unsigned char *rawData = nil;
rawData = (unsigned char *)[self convertUIImageToBitmapRGBA8:myUIImage];

grid = malloc(sizeof(float)*(bufferLength/4));

NSString *hexColor = nil;

for (int i = 0 ; i < (bufferLength); i=i+4)
{
hexColor = [NSString stringWithFormat: @"%02x%02x%02x", (int)(rawData[i + 0]),(int)(rawData[i + 1]),(int)(rawData[i + 2])];


//mess with colors how you see fit - I just detected certain colors and slapped 
//that into an array of floats which I later put over my mapview much like the 
//hazardmap example from apple.

if ([hexColor isEqualToString:@"ff0299"]) //pink
    value = (float)11;
if ([hexColor isEqualToString:@"9933cc"]) //purple
    value = (float)12;

//etc...

grid[i/4] = value;
}

また、ここからいくつかのメソッド(つまり、convertUIImageToBitmapRGBA8)を借りました:https ://gist.github.com/739132

これが誰かを助けることを願っています!

于 2011-02-19T02:47:14.720 に答える