0
A
回答
1
/**
* Structure to keep one pixel in RRRRRRRRGGGGGGGGBBBBBBBBAAAAAAAA format
*/
struct pixel {
unsigned char r, g, b, a;
};
/**
* Process the image and return the number of pixels in it.
*/
- (NSUInteger) processImage: (UIImage*) image withRed:(NSUInteger)r green:(NSUInteger)g blue:(NSUInteger)b
{
NSUInteger numberOfPixels = 0;
// Allocate a buffer big enough to hold all the pixels
struct pixel* pixels = (struct pixel*) calloc(1, image.size.width * image.size.height * sizeof(struct pixel));
if (pixels != nil)
{
// Create a new bitmap
CGContextRef context = CGBitmapContextCreate(
(void*) pixels,
image.size.width,
image.size.height,
8,
image.size.width * 4,
CGImageGetColorSpace(image.CGImage),
kCGImageAlphaPremultipliedLast
);
if (context != NULL)
{
// Draw the image in the bitmap
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, image.size.width, image.size.height), image.CGImage);
// Now that we have the image drawn in our own buffer, we can loop over the pixels to
// process it. This simple case simply counts all pixels that have a pure red component.
// There are probably more efficient and interesting ways to do this. But the important
// part is that the pixels buffer can be read directly.
NSUInteger p = image.size.width * image.size.height;
while (p > 0) {
if (pixels->r == r && pixels->g == g && pixels->b == b) {
numberOfPixels++;
}
pixels++;
p--;
}
CGContextRelease(context);
}
free(pixels);
}
return numberOfPixels;
}
用途:
NSUInteger numberOfSpecificColorPixels = [self processImage: [UIImage imageNamed: @"testImage.png" withRed:232 green:212 blue:192]];
这会给你的特定颜色的像素,然后你就可以按照您的要求
相关问题
- 1. iPhone - 在将相机图片保存到iPhone相册时崩溃
- 2. 如何从iPhone设备照片库中的相机中保存拍摄图像?
- 3. 如何从图片框中获取特定像素的颜色?
- 4. 如何从相机中抓取图片?
- 5. Highcharts与饼图切片颜色相同的图例颜色
- 6. 如何从图片中自定义背景颜色
- 7. 如何确定winforms中给定颜色的禁用颜色?
- 8. iphone:如何从图像中移除红色? (RGB颜色)
- 9. 如何在matplotlib颜色映射中间隔确定颜色
- 10. 如何自动确定与白色相似的颜色?
- 11. 填写图片中的特定颜色
- 12. 如何在Adobe Photoshop中从确切的颜色更改为精确的颜色?
- 13. 如何从Java中的一组选定颜色中输出随机颜色? (Android)
- 14. 在SD卡中保存相机图片
- 15. 如何在iPhone SDK中随机更改图像视图的背景颜色?
- 16. 随机颜色从颜色
- 17. 相机意图图片只保存在相机相册
- 18. 删除从位图图片的颜色?
- 19. 在iphone中自定义相机视图
- 20. 如何确定给定颜色的颜色转换?
- 21. 如何在相册中存储从相机捕获的图像?
- 22. 确定一种颜色“图像中单个颜色的多少”
- 23. 从图片中获取像素颜色?
- 24. 调整颜色段的颜色图片
- 25. 如何从颜色数组中随机设置jbutton颜色?
- 26. 在Android中从相机中保存图片
- 27. 如何在iPhone照片库中保存图像只有自定义相册?
- 28. 如何防止再次从照相机保存图片
- 29. 保存覆盖图像的相机照片 - iphone SDK
- 30. 如何确定图像中的最大颜色?
你是男人使用的号码,我欠你一个啤酒。 – Dalton
感谢高兴它帮助:) – Usama