c++ - 使用蒙版获取图像像素

标签 c++ ios objective-c cocos2d-iphone shader

所以我有一些原始图像。

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

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

很高兴收到任何建议。

谢谢

最佳答案

您可以将您的图像放在 UIImageView 中,并使用类似的东西向该 ImageView 添加 mask

myImageView.layer.mask = myMaskLayer;

要绘制您的自定义形状,myMaskLayer 应该是您自己的 CALayer 子类的一个实例,该子类实现了 drawInContext 方法。该方法应该使用完整的 alpha 绘制要在图像中显示的区域,并使用零 alpha 绘制要隐藏的区域(如果您愿意,也可以在 alpha 之间使用)。下面是 CALayer 子类的示例实现,当用作 ImageView 上的 mask 时,将导致仅显示图像左上角的椭圆区域:

@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

要将这样的 mask 层应用于 ImageView ,您可以使用这样的代码

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);

关于c++ - 使用蒙版获取图像像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15609170/

相关文章:

c++ - "class template"与 "template class"

ios - 为 iOS 应用程序创建简单的数据库

ios - UIScrollView 不使用 UItextfields 滚动

ios - 更改 View 变换会更改 iOS 7 上的 View 位置,但不会更改 ios8

ios - 标签栏项目图像不显示 - xcode 6

c++ - “_snprintf”未在此范围内声明

c++ - 使用 C++ 模板从成员变量进行接口(interface)转发

C++ 显式返回类型模板特化

objective-c - 在 Objective-C 应用程序中保留 session ID

ios - 我的 UIButtonBarItem 's don' t 使用 FlexibleSpace 正确布局