我需要尽快从图像中获取RBG数据。我目前这样做:
CGRect rect = CGRectMake(roundf(xCoord * self.scale), roundf(yCoord * self.scale), 1, 1);
CGImageRef cropped = CGImageCreateWithImageInRect(self.CGImage, rect);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char rawData[UIIMAGE_BYTES_PER_PIXEL];
NSUInteger bytesPerRow = UIIMAGE_BYTES_PER_PIXEL;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, 1, 1,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, 1, 1), cropped);
CGImageRelease(cropped);
CGContextRelease(context);
CGFloat red = rawData[0] / 255.0;
CGFloat green = rawData[1] / 255.0;
CGFloat blue = rawData[2] / 255.0;
CGFloat alpha = rawData[3] / 255.0;
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
但它太慢了。
有没有人有一个使用Open GL纹理代替的代码片段?我已经看了一下GPUImage项目试图获得一个起点,但是为了这一点而必须学习所有关于OpenGL的东西是压倒性的。
答案 0 :(得分:1)
Justin指出,这里最好的方法是一次性获取所有像素数据,然后访问它。像我一样一次获取一个像素的数据非常慢。