1

私のアプリケーションでは、この上に透明な背景色のUIImageView1 つがあります。UIViewUIImageView

ユーザーがof にUIBezierPath描画しているように見えるように、この UIView でフリーハンド描画を使用しています。UIImageUIImageView

色に透明感をプラスしていUIBezierPathます。

私の質問は、最初にグレースケールの外観を作成する方法をロードUIImageしたときで、ユーザーが画面上で指を動かすと、元の色がユーザーに表示されますか?UIImageViewUIImageUIImage

4

2 に答える 2

0

タッチすると、この関数を呼び出して、グレースケールの画像を返します。タッチが終了したら、UIImageViewの元の画像を設定します。

- (UIImage*)imageWithGrayScaleImage:(UIImage*)inputImg
{   
//Creating image rectangle
//CGRect imageRect = CGRectMake(0, 0, inputImg.size.width / (CGFloat)[inputImg scale], inputImg.size.height / (CGFloat)[inputImg scale]);
CGRect imageRect = CGRectMake(0, 0, inputImg.size.width, inputImg.size.height);

//Allocating memory for pixels
int width = imageRect.size.width;
int height = imageRect.size.height;

uint32_t *pixels = (uint32_t*)malloc(width * height * sizeof(uint32_t));

//Clearing the memory to preserve any transparency.(Alpha)
memset(pixels, 0, width * height * sizeof(uint32_t));

//Creating a context with RGBA pixels
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);

//Drawing the bitmap to the context which will fill the pixels memory
CGContextDrawImage(context, imageRect, [inputImg CGImage]);

//Indices as ARGB or RGBA
const int RED = 1;
const int GREEN = 2;
const int BLUE = 3;

for (int y = 0; y < height; y++)
{
    for (int x = 0; x < width; x++)
    {
        uint8_t* rgbaPixel = (uint8_t*)&pixels[y * width + x];

        //Calculating the grayScale value
        uint32_t grayPixel = 0.3 * rgbaPixel[RED] + 0.59 * rgbaPixel[GREEN] + 0.11 * rgbaPixel[BLUE];

        //Setting the pixel to gray
        rgbaPixel[RED] = grayPixel;
        rgbaPixel[GREEN] = grayPixel;
        rgbaPixel[BLUE] = grayPixel;
    }
}

//Creating new CGImage from the context with modified pixels
CGImageRef newCGImage = CGBitmapContextCreateImage(context);

//Releasing resources to free up memory
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
free(pixels);

//Creating UIImage for return value
//UIImage* newUIImage = [UIImage imageWithCGImage:newCGImage scale:(CGFloat)[inputImg scale] orientation:UIImageOrientationUp];
UIImage* newUIImage = [UIImage imageWithCGImage:newCGImage];

//Releasing the CGImage
CGImageRelease(newCGImage);

return newUIImage;
}
于 2012-07-27T12:06:06.933 に答える
0

これを行う適切な方法は、Core Image を使用し、カラー イメージで動作する独自の Core Image フィルタを作成することです。フィルターにいくつかのプロパティを設定することで、一部のセクションをカラーで描画し、他の領域をグレー スケールに変換することがわかります。

とはいえ、これは簡単な作業項目ではありません。他の何かを一緒にハックできるかもしれませんが、おそらくぎくしゃくして滑らかではありません. Core Image フィルターは、Core Animation と同じレベル (GPU の近く) で機能するため、そのようにすると非常にうまく機能します。

于 2012-07-27T11:28:42.817 に答える