2009-12-21 5 views
3

색상이 객체로 배열되어 있습니다. 나는 그것에서 이미지를 만들고 싶다. 사실, 무슨 일이 일어나고, 나는 이미지의 각 픽셀의 픽셀 값을 가져 와서 수정하고 해당 객체를 변경할 수있는 배열에 저장합니다. 이제 이것으로 이미지를 그려야합니다. 그렇게하는 방법?? 어떤 아이디어?iphone에서 색상 배열로 이미지 만들기

-(UIImage*)modifyPixels:(UIImage*)originalImage { 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init]; 
    NSMutableArray *result =[[NSMutableArray alloc]init]; 
    int width = img.size.width; 
    int height = img.size.height; 
     originalImage = imageView.image; 

    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),img.CGImage); 
    CGContextRelease(context); 
    int byteIndex = 0; 
    for (int xx=0;xx<width;xx++){ 
     for (int yy=0;yy<height;yy++){ 
    // Now rawData contains the image data in the RGBA8888 pixel format. 
      NSLog(@"(%d,%d)",xx,yy); 
    NSLog(@"Alpha 255-Value is: %u", rawData[byteIndex + 3]); 
    NSLog(@"Red 255-Value is: %u", rawData[byteIndex]); 
    NSLog(@"Green 255-Value is: %u",rawData[byteIndex + 1]); 
    NSLog(@"Blue 255-Value is: %u",rawData[byteIndex + 2]); 
      CGFloat red = (rawData[byteIndex]+rawData[byteIndex + 1]+rawData[byteIndex + 2])/3; 
      CGFloat green = red; 
      CGFloat blue = red; 
      CGFloat alpha = 255; 
      byteIndex += 4; 

      UIColor *acolor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha]; 
      [result addObject:acolor]; 
       } 

     } 
     UIImage *newImage; 
     //CREATE NEW UIIMAGE (newImage) HERE from acolor(array of colors) 
//this is the portion i'm in trouble with 

     return newImage;  
     [pool release]; 
    } 
+1

[풀 릴리스]는 도달 할 수없는 코드이며 누수가 발생합니다. 어쨌든 메서드는 자체 풀이 필요하지 않습니다. Xcode 프로젝트에서 Clang 정적 분석 도구를 사용할 수도 있습니다. (그룹 트리에서 프로젝트의 '정보 입수 ->'정적 분석 실행 ') –

답변

2

나는 모든 채널 평균을 시도합니다. 결과는 rawImage에 저장됩니다

CGImageRef inImage = mainImageView.image.CGImage;   
CFDataRef dataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage)); 
UInt8* pixelBuffer = (UInt8*)CFDataGetBytePtr(dataRef); 
int length = CFDataGetLength(dataRef); 
for (int index = 0; index < length; index += 4) 
{ 
    pixelBuffer[index + 1] = (pixelBuffer[index + 1] + pixelBuffer[index + 2] + pixelBuffer[index + 3])/3.0; 
    pixelBuffer[index + 2] = pixelBuffer[index + 1]; 
    pixelBuffer[index + 3] = pixelBuffer[index + 1];  
} 
CGContextRef ctx = CGBitmapContextCreate(pixelBuffer, 
             CGImageGetWidth(inImage), 
             CGImageGetHeight(inImage), 
             8, 
             CGImageGetBytesPerRow(inImage), 
             CGImageGetColorSpace(inImage), 
             kCGImageAlphaPremultipliedFirst); 
CGImageRef imageRef = CGBitmapContextCreateImage(ctx); 
UIImage* rawImage = [UIImage imageWithCGImage:imageRef]; 
CGContextRelease(ctx); 
CFRelease(dataRef); 
CGImageRelease(imageRef); 

:
당신은 코어 그래픽을 사용하는 다음 방법을 시도 할 수 있습니다.

GLImageProcessing sample from Apple을 살펴볼 수도 있습니다.
이것은 OpenGL을 사용하는 iPhone의 기본 이미지 처리 기술을 보여줍니다.