2013-03-14 4 views
0

안녕 얘들 아 나는 현재 UIImage의 모든 픽셀을 반복하려고하지만 그것을 구현하는 방법 sooo 많은 시간이 걸립니다. 그래서 나는 그것을 잘못 구현 한 방법이라고 생각했다. 내가 픽셀의 RGBA 값 얼마나 이 내 방법 :UIImage의 모든 픽셀을 반복하는 방법은 무엇입니까?

+(NSArray*)getRGBAsFromImage:(UIImage*)image atX:(int)xx andY:(int)yy count:(int)count 
{ 
    // Initializing the result array 
    NSMutableArray *result = [NSMutableArray arrayWithCapacity:count]; 

    // First get the image into your data buffer 
    CGImageRef imageRef = [image CGImage];      // creating an Instance of 
    NSUInteger width = CGImageGetWidth(imageRef);    // Get width of our Image 
    NSUInteger height = CGImageGetHeight(imageRef);    // Get height of our Image 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); // creating our colour Space 

    // Getting that raw Data out of an image 
    unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char)); 


    NSUInteger bytesPerPixel = 4;        // Bytes per pixel defined 
    NSUInteger bytesPerRow = bytesPerPixel * width;    // Bytes per row 
    NSUInteger bitsPerComponent = 8;       // Bytes per component 

    CGContextRef context = CGBitmapContextCreate(rawData, width, height, 
               bitsPerComponent, bytesPerRow, colorSpace, 
               kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); 
    CGColorSpaceRelease(colorSpace); // releasing the color space 

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

    // Now your rawData contains the image data in the RGBA8888 pixel format. 
    int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel; 
    for (int ii = 0 ; ii < count ; ++ii) 
    { 
     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; 

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

    free(rawData); 
    return result; 
} 

을 그리고 이것은 내가 모든 픽셀을 분석하는 방법 코드입니다 : 모두를 얻기의 더 빠른 방법이 있나요

uiimage 인스턴스의 RGBA 값?

답변

1

모든 픽셀에 대해 이미지의 새 복사본을 생성 한 다음이를 버리고 있습니다. 예. 데이터를 한 번 가져온 다음 해당 바이트 배열을 처리하면 훨씬 빠릅니다.

하지만 "무언가를하십시오"란 무엇인가에 달려 있습니다. 매우 신속하게 이미지 처리를 할 수있는 많은 CoreImage 및 vImage 기능이 있지만 문제를 다르게 접근해야 할 수 있습니다. 그것은 당신이하는 일에 달려 있습니다.

관련 문제