2011-02-08 18 views
0

私は客観的に少し新しく、Quartz 2Dでのプログラミングでも新しいので、事前にお詫び申し上げます。私はUIImageから特定の色(一色だけではない)を削除する方法があります。CGImageCreateWithMaskingColorsを使用して複数のカラーマスクを適用するにはどうすればよいですか?

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; 
} 

答えて

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

希望これが誰かを助け!

関連する問題