2012-11-08 24 views
0

例如,我有一个UIImage,我想知道图像位置(300,200)是哪种颜色,我该怎么办?谢谢。如何在UIImage中获取指定点的颜色?

+2

检查此问题 - http://stackoverflow.com/questions/448125/how-to-get-pixel-data-from-a-uiimage-cocoa-touch-or-cgimage-core-graphics – rishi

+0

你应该尝试亚光在这里描述的技术:http://stackoverflow.com/questions/1042830/retrieving-a-pixel-alpha-value-for-a-uiimage - 当然,使用颜色目标位图。好处是您不需要分配和绘制整个图像来访问一个像素的值。 – justin

回答

3

调用此方法的形式uitouch移动..

-(void) getPixelColorAtLocation:(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]; 

    // NSLog(@"x- %f y- %f",point.x,point.y); 

    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace); 

    NSLog(@RGB Color code "%d %d %d",pixel[0],pixel[1],pixel[2]); 
} 

你可以得到触摸点的颜色代码RGB COLR组合。 试试吧

相关问题