2013-05-07 142 views
0

我想獲取圖像中所有單個像素的顏色。 詳細說明 假設我有一個名爲「SampleImage」的圖像,具有400 x 400像素 基本上我想從'SampleImage'創建一個網格,它將有400 x 400個正方形,每個正方形填充對應於特定像素的顏色'SampleImage'。iPhone:如何獲取圖像的每個像素的顏色?

我知道這是一個有點抽象,但我在iOS的新手,不知道從哪裏開始。 在此先感謝!

+0

http://stackoverflow.com/questions/1160229/how-to-get-the-color-of-a-pixel-in-an- uiview可能的重複。 – Buntylm 2013-05-07 10:12:28

回答

2

此代碼工作得十分完美,我 - :

- (NSArray*)getRGBAsFromImage:(UIImage*)image atX:(int)xx andY:(int)yy count:(int)count{ 
    NSMutableArray *result = [NSMutableArray arrayWithCapacity:count]; 

    // First get the image into your data buffer 
    CGImageRef imageRef = [image CGImage]; 
    NSUInteger width = CGImageGetWidth(imageRef); 
    NSUInteger height = CGImageGetHeight(imageRef); 
    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); 

    // 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; 
} 
2

使用此: 這是更有效的解決方案:

// UIView+ColorOfPoint.h 
@interface UIView (ColorOfPoint) 
- (UIColor *) colorOfPoint:(CGPoint)point; 
@end 

// UIView+ColorOfPoint.m 
#import "UIView+ColorOfPoint.h" 
#import <QuartzCore/QuartzCore.h> 

@implementation UIView (ColorOfPoint) 

- (UIColor *) colorOfPoint:(CGPoint)point 
{ 
    unsigned char pixel[4] = {0}; 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 
    CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGImageAlphaPremultipliedLast); 

    CGContextTranslateCTM(context, -point.x, -point.y); 

    [self.layer renderInContext:context]; 

    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace); 

    //NSLog(@"pixel: %d %d %d %d", pixel[0], pixel[1], pixel[2], pixel[3]); 

    UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0]; 

    return color; 
} 

@end 

希望它可以幫助你。

+0

謝謝Nishant和borrrden! @Nishant:我必須在哪裏指定必須選擇顏色的圖像? 我是否必須一次又一次地爲每個像素調用此方法,或者像上面提到的borrrden一樣縮寫? 再次感謝! – 2013-05-07 10:27:35

1

如果你是新手,你應該考慮先做些簡單的事情。無論如何,你需要做的是通過CGBitmapContextCreate設置一個CGContextRef,並有足夠的數據來保存你的圖像。一旦你創建它,你需要通過CGDrawImage將圖像呈現給它。之後,您將擁有指向圖像中每個像素的指針。該代碼類似於Nishant的答案,但不是1x1,您將使用400x400一次獲取所有像素。

相關問題