2012-01-25 23 views
1

UIImageをグレースケールに変更するコードに問題があります。これはiPhone/iPod上で正しく機能しますが、既に描画されているものであれば、iPad上ではすべてが伸びて歪んでしまいます。iOSグレースケール/白黒

また、のimageRef = CGBitmapContextCreateImage(ctx)のiPadでのみクラッシュすることがあります。ここで

はコードです:

CGContextRef ctx; 
CGImageRef imageRef = [self.drawImage.image CGImage]; 
NSUInteger width = CGImageGetWidth(imageRef); 
NSUInteger height = CGImageGetHeight(imageRef); 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
unsigned char *rawData = malloc(height * width * 4); 
NSUInteger bytesPerPixel = 4; 
NSUInteger bytesPerRow = bytesPerPixel * width; 
NSUInteger bitsPerComponent = 8; 
CGContextRef context = CGBitmapContextCreate(rawData, width, height, 
              bitsPerComponent, bytesPerRow, colorSpace, 
              kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 
CGColorSpaceRelease(colorSpace); 

CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef); 
CGContextRelease(context); 

int byteIndex = 0; 
int grayScale; 

for(int ii = 0 ; ii < width * height ; ++ii) 
{ 
    grayScale = (rawData[byteIndex] + rawData[byteIndex + 1] + rawData[byteIndex + 2])/3; 

    rawData[byteIndex] = (char)grayScale; 
    rawData[byteIndex+1] = (char)grayScale; 
    rawData[byteIndex+2] = (char)grayScale; 
    //rawData[byteIndex+3] = 255; 

    byteIndex += 4; 
} 


ctx = CGBitmapContextCreate(rawData, 
          CGImageGetWidth(imageRef), 
          CGImageGetHeight(imageRef), 
          8, 
          CGImageGetBytesPerRow(imageRef), 
          CGImageGetColorSpace(imageRef), 
          kCGImageAlphaPremultipliedLast); 

imageRef = CGBitmapContextCreateImage (ctx); 
UIImage* rawImage = [UIImage imageWithCGImage:imageRef]; 

CGContextRelease(ctx); 

self.drawImage.image = rawImage; 

free(rawData); 
+0

iPadでiPhoneで使用しているのと同じ画像を使用するとどうなりますか?私は画像の形式や何かに違いがあると思う。また、グレースケールに変換するアルゴリズムは、赤の値の30%、緑の値の59%、青の値の11%を加算するアルゴリズムが優れていることに注意してください。grayScale =((rawData [byteIndex] * 0.3f)+(rawData [byteIndex + 1] * 0.59)+(rawData [byteIndex + 2] * 0.11)); –

+0

画像フォーマットは毎回同じです。あなたが好きなように描画することができ、次にグレースケールボタンを押すことができます。それはちょうどiPad上で歪められ、しばしばimageRef = CGBitmapContextCreateImage(ctx)でクラッシュする。 iPadのみ –

答えて

1

は、他の場所で

が使用されて余分なコンテキストを取り除くと

CGContextRef ctx; 
CGImageRef imageRef = [self.drawImage.image CGImage]; 
NSUInteger width = CGImageGetWidth(imageRef); 
NSUInteger height = CGImageGetHeight(imageRef); 
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
unsigned char *rawData = malloc(height * width * 4); 
NSUInteger bytesPerPixel = 4; 
NSUInteger bytesPerRow = bytesPerPixel * width; 
NSUInteger bitsPerComponent = CGImageGetBitsPerComponent(imageRef); 

ctx = CGBitmapContextCreate(rawData, 
          width, 
          height, 
          bitsPerComponent, 
          bytesPerRow, 
          colorSpace, 
          kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host); 

CGContextDrawImage(ctx, CGRectMake(0, 0, width, height), imageRef); 

int byteIndex = 0; 
int grayScale; 

for(int ii = 0 ; ii < width * height ; ++ii) 
{ 
    grayScale = (rawData[byteIndex] + rawData[byteIndex + 1] + rawData[byteIndex + 2])/3; 

    rawData[byteIndex] = (char)grayScale; 
    rawData[byteIndex+1] = (char)grayScale; 
    rawData[byteIndex+2] = (char)grayScale; 
    //rawData[byteIndex+3] = 255; 

    byteIndex += 4; 
} 


imageRef = CGBitmapContextCreateImage(ctx); 
UIImage* rawImage = [UIImage imageWithCGImage:imageRef]; 

CGColorSpaceRelease(colorSpace); 
CGContextRelease(ctx); 

self.drawImage.image = rawImage; 

free(rawData); 
2

ビットマップ形式を変更し手に入れたいくつかの助けを借りてそれを考え出し活用を検討しますCIColorMonochromeフィルタはiOS 6で利用できます。

void ToMonochrome() 
{ 
    var mono = new CIColorMonochrome(); 
    mono.Color = CIColor.FromRgb(1, 1, 1); 
    mono.Intensity = 1.0f; 
    var uiImage = new UIImage("Images/photo.jpg"); 
    mono.Image = CIImage.FromCGImage(uiImage.CGImage); 
    DisplayFilterOutput(mono, ImageView); 
} 

static void DisplayFilterOutput(CIFilter filter, UIImageView imageView) 
{ 
    CIImage output = filter.OutputImage; 
    if (output == null) 
     return; 
    var context = CIContext.FromOptions(null); 
    var renderedImage = context.CreateCGImage(output, output.Extent); 
    var finalImage = new UIImage(renderedImage); 
    imageView.Image = finalImage; 
} 
関連する問題