使用蒙版获取图像像素

问题描述:

所以我有一些原始图像。使用蒙版获取图像像素

我需要使用特定的蒙版获取并显示此图像的某些部分。蒙版不是矩形或形状。它包含不同的多边形和形状。

是否有任何方法或教程如何实现?或从哪里开始做到这一点?我应该写一些小的着色器或比较图像的适当像素或类似的东西?

对任何建议感到高兴。

谢谢

你可以把你的形象在一个UIImageView和掩码添加到图像视图的东西,如

myImageView.layer.mask = myMaskLayer; 

要绘制自定义形状,myMaskLayer应该是你自己的一个实例CALayer的子类实现了drawInContext方法。该方法应该绘制区域以完整的alpha值显示在图像中,并且要用零alpha来隐藏区域(并且如果您喜欢,也可以在alpha之间使用)。这里的一个的CALayer子类的一个示例实现中,如在图像视图的掩模一起使用时,将导致仅一个椭圆区域中的图像的左上角显示:

@interface MyMaskLayer : CALayer 
@end 

@implementation MyMaskLayer 
- (void)drawInContext:(CGContextRef)ctx { 
    static CGColorSpaceRef rgbColorSpace; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     rgbColorSpace = CGColorSpaceCreateDeviceRGB(); 
    }); 

    const CGRect selfBounds = self.bounds; 
    CGContextSetFillColorSpace(ctx, rgbColorSpace); 
    CGContextSetFillColor(ctx, (CGFloat[4]){0.0, 0.0, 0.0, 0.0}); 
    CGContextFillRect(ctx, selfBounds); 
    CGContextSetFillColor(ctx, (CGFloat[4]){0.0, 0.0, 0.0, 1.0}); 
    CGContextFillEllipseInRect(ctx, selfBounds); 
} 
@end 

要应用这种掩模层的形象图,你可以使用这样的代码

MyMaskLayer *maskLayer = [[MyMaskLayer alloc] init]; 
maskLayer.bounds = self.view.bounds; 
[maskLayer setNeedsDisplay]; 
self.imageView.layer.mask = maskLayer; 

现在,如果你想从这样的渲染,最好渲染成一个CGContext上由你控制像素缓冲区的位图支持得到的像素数据。渲染后,您可以检查缓冲区中的像素数据。下面是一个例子,我创建了一个位图上下文,并在该位图上下文中渲染一个图层和蒙版。由于我为像素数据提供了自己的缓冲区,因此我可以在缓冲区中四处寻找渲染后的RGBA值。

const CGRect imageViewBounds = self.imageView.bounds; 
const size_t imageWidth = CGRectGetWidth(imageViewBounds); 
const size_t imageHeight = CGRectGetHeight(imageViewBounds); 
const size_t bytesPerPixel = 4; 
// Allocate your own buffer for the bitmap data. You can pass NULL to 
// CGBitmapContextCreate and then Quartz will allocate the memory for you, but 
// you won't have a pointer to the resulting pixel data you want to inspect! 
unsigned char *bitmapData = (unsigned char *)malloc(imageWidth * imageHeight * bytesPerPixel); 
CGContextRef context = CGBitmapContextCreate(bitmapData, imageWidth, imageHeight, 8, bytesPerPixel * imageWidth, rgbColorSpace, kCGImageAlphaPremultipliedLast); 
// Render the image into the bitmap context. 
[self.imageView.layer renderInContext:context]; 
// Set the blend mode to destination in, pixels become "destination * source alpha" 
CGContextSetBlendMode(context, kCGBlendModeDestinationIn); 
// Render the mask into the bitmap context. 
[self.imageView.layer.mask renderInContext:context]; 
// Whatever you like here, I just chose the middle of the image. 
const size_t x = imageWidth/2; 
const size_t y = imageHeight/2; 
const size_t pixelIndex = y * imageWidth + x; 
const unsigned char red = bitmapData[pixelIndex]; 
const unsigned char green = bitmapData[pixelIndex + 1]; 
const unsigned char blue = bitmapData[pixelIndex + 2]; 
const unsigned char alpha = bitmapData[pixelIndex + 3]; 
NSLog(@"rgba: {%u, %u, %u, %u}", red, green, blue, alpha); 
+0

谢谢你真的,它的显示和获取像素? – 2013-03-25 07:14:51

+0

@George没问题。我已经添加了一些示例代码来从像这样的设置中提取像素数据。 – 2013-03-25 07:41:51

+0

我想你救了我的一天,甚至超过一天!! :) – 2013-03-25 08:25:32