2012-07-09 4 views
0

내 목표는 다음과 같습니다.아이폰 픽셀 RGBA 값 편집

플레이어가 화면을 터치하면 프로그램은 터치 한 위치의 픽셀 값을 찾아 모든 픽셀을 반복해야하며 정확한 RGBA 값을 가진 모든 것은 편집됩니다. 알파를 모두 보이지 않게하려면 0으로 설정하고 싶습니다.

지금까지 나는 그 정보를 은퇴 할 수 있었지만 어떤 것들은 터치 된 픽셀에 해당하는지 알아 냈습니다. 그러나 픽셀을 설정하고 새 데이터에서 UIImage를 만들고 설정했습니다. 아무것도 바뀌지 않습니다.

약간의 도움을 많이 주시면 감사하겠습니다. (있는 UIImage *) 화상 ATX : (INT) XX ANDY (INT) YY

// First get the image into your data buffer 
CGImageRef imageRef = [image CGImage]; 

NSUInteger width = CGImageGetWidth(imageRef); 
NSUInteger height = CGImageGetHeight(imageRef); 

//convert phone screen coords to texture coordinates. 
xx *= width/[[UIScreen mainScreen] bounds].size.width; 
yy *= height/[[UIScreen mainScreen] bounds].size.height; 

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char)); 

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

/*        MY STUFF          */ 
int counter = 0; 
/*        MY STUFF          */ 

// Now your rawData contains the image data in the RGBA8888 pixel format. 
// Get the byteIndex of pixel tapped. 

int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel; 

CGFloat red = (rawData[byteIndex]  * 1.0)/255.0; 
CGFloat green = (rawData[byteIndex + 1] * 1.0)/255.0; 
CGFloat blue = (rawData[byteIndex + 2] * 1.0)/255.0; 
CGFloat alpha = (rawData[byteIndex + 3] * 1.0)/255.0; 
byteIndex += 4; 

for(int x = 0; x < width; x++) 
{ 
    for(int y = 0; y < height; y++) 
    { 
     byteIndex = (x + (y * width)) * bytesPerPixel; 

     CGFloat redVal = (rawData[byteIndex]  * 1.0)/255.0; 
     CGFloat greenVal = (rawData[byteIndex + 1] * 1.0)/255.0; 
     CGFloat blueVal = (rawData[byteIndex + 2] * 1.0)/255.0; 
     CGFloat alphaVal = (rawData[byteIndex + 3] * 1.0)/255.0; 
     byteIndex += 4; 

     if(alphaVal != 0) 
     { 
      if(redVal == red && greenVal == green && blueVal == blue) 
      { 
       rawData[byteIndex]  = 255; 
       rawData[byteIndex + 1] = 255; 
       rawData[byteIndex + 2] = 255; 
       rawData[byteIndex + 3] = 1; 
       counter ++; 
      } 
     } 
    } 
} 

UIImage *newImage = [UIImage imageWithCGImage: imageRef]; 
image = newImage; 

NSLog(@"Pixels amount: %i", counter); 


free(rawData); 

return image; 

및 호출 될

코드

함수

가 가

(있는 UIImage *) getRGBAsFromImage가 을

이하 like so

tempBGRef.image = [MainMenuViewController getRGBAsFromImage:[tempBGRef image] atX:touchPointInView.x andY:touchPointInView.y]; 

답변

0

rawData 개체를 수정하지만 imageRef에 포함 된 데이터가 아닙니다. rawData 객체에 imageRef 데이터를 복사했지만 rawData 값을 변경할 때 imageRef가 업데이트되지 않습니다. CGImageCreate 메서드를 호출하여 rawData 배열에서 새 CGImage 객체를 만들어야합니다.

+0

환호 ... 불행히도 나는 알아야 할 많은 분야가 있습니다. 코드 샘플로 raw 데이터 배열 – Genhain

+0

Google을 사용하는 경우가 아니면 - 많이 있어야합니다. –

0

나는 당신이보아야하는 당신의 목표에 의한 것이라고 생각한다 this

페이지가 중국어로되어 있지만 녹색 상자를 열면 zip 파일을 다운로드해야합니다. 왜곡 기법을 사용하여 배경을 편집합니다. 터치 감지를 살펴보면 프로젝트의 기초가 될 것입니다.

도움이 되었기를 바랍니다.