• <tfoot id='ngmUD'></tfoot>
        <bdo id='ngmUD'></bdo><ul id='ngmUD'></ul>

    1. <legend id='ngmUD'><style id='ngmUD'><dir id='ngmUD'><q id='ngmUD'></q></dir></style></legend>

      <small id='ngmUD'></small><noframes id='ngmUD'>

        <i id='ngmUD'><tr id='ngmUD'><dt id='ngmUD'><q id='ngmUD'><span id='ngmUD'><b id='ngmUD'><form id='ngmUD'><ins id='ngmUD'></ins><ul id='ngmUD'></ul><sub id='ngmUD'></sub></form><legend id='ngmUD'></legend><bdo id='ngmUD'><pre id='ngmUD'><center id='ngmUD'></center></pre></bdo></b><th id='ngmUD'></th></span></q></dt></tr></i><div id='ngmUD'><tfoot id='ngmUD'></tfoot><dl id='ngmUD'><fieldset id='ngmUD'></fieldset></dl></div>

        使用蒙版获取图像像素

        Get image pixels using mask(使用蒙版获取图像像素)
              <bdo id='S20ip'></bdo><ul id='S20ip'></ul>

                1. <tfoot id='S20ip'></tfoot>

                  <small id='S20ip'></small><noframes id='S20ip'>

                  <legend id='S20ip'><style id='S20ip'><dir id='S20ip'><q id='S20ip'></q></dir></style></legend>

                    <tbody id='S20ip'></tbody>

                  <i id='S20ip'><tr id='S20ip'><dt id='S20ip'><q id='S20ip'><span id='S20ip'><b id='S20ip'><form id='S20ip'><ins id='S20ip'></ins><ul id='S20ip'></ul><sub id='S20ip'></sub></form><legend id='S20ip'></legend><bdo id='S20ip'><pre id='S20ip'><center id='S20ip'></center></pre></bdo></b><th id='S20ip'></th></span></q></dt></tr></i><div id='S20ip'><tfoot id='S20ip'></tfoot><dl id='S20ip'><fieldset id='S20ip'></fieldset></dl></div>
                  本文介绍了使用蒙版获取图像像素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  所以我有一些原始图像.

                  So i have some original image.

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

                  I need to get and display some part of this image using specific mask. Mask is not a rectangle or a shape. It contains different polygons and shapes.

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

                  Are there any methods or tutorials how to implement that? Or from where to start to make this? Shall i write some small shader or compare appropriate pixels of the images or something like this?

                  很高兴收到任何建议.

                  谢谢

                  推荐答案

                  你可以把你的图片放在一个 UIImageView 中,然后给那个图片视图添加一个掩码,比如

                  You can put your image in a UIImageView and add a mask to that image view with something like

                  myImageView.layer.mask = myMaskLayer;
                  

                  要绘制您的自定义形状,myMaskLayer 应该是您自己的实现 drawInContext 方法的 CALayer 子类的实例.该方法应该使用完整的 alpha 绘制要在图像中显示的区域以及使用零 alpha 隐藏的区域(如果您愿意,也可以在 alpha 之间使用).这是一个 CALayer 子类的示例实现,当它用作图像视图上的掩码时,将只显示图像左上角的椭圆形区域:

                  To draw your custom shapes, myMaskLayer should be an instance of your own subclass of CALayer that implements the drawInContext method. The method should draw the areas to be shown in the image with full alpha and the areas to be hidden with zero alpha (and can also use in between alphas if you like). Here's an example implementation of a CALayer subclass that, when used as a mask on the image view, will cause only an oval area in the top left corner of the image to be shown:

                  @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
                  

                  要将这样的遮罩层应用于图像视图,您可以使用这样的代码

                  To apply such a mask layer to an image view, you might use code like this

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

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

                  Now, if you want to get pixel data from a rendering like this it's best to render into a CGContext backed by a bitmap where you control the pixel buffer. You can inspect the pixel data in your buffer after rendering. Here's an example where I create a bitmap context and render a layer and mask into that bitmap context. Since I supply my own buffer for the pixel data, I can then go poking around in the buffer to get RGBA values after rendering.

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

                  这篇关于使用蒙版获取图像像素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)
                  Cocos2d - How to check for Intersection between objects in different layers(Cocos2d - 如何检查不同层中对象之间的交集)
                  Resume game cocos2d(恢复游戏 cocos2d)
                  Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)
                  How to convert 32 bit PNG to RGB565?(如何将 32 位 PNG 转换为 RGB565?)
                  Proper cocos2d scene restart?(正确的 cocos2d 场景重启?)
                  <tfoot id='91Mbs'></tfoot>
                  <legend id='91Mbs'><style id='91Mbs'><dir id='91Mbs'><q id='91Mbs'></q></dir></style></legend>
                  • <bdo id='91Mbs'></bdo><ul id='91Mbs'></ul>

                        <tbody id='91Mbs'></tbody>
                        <i id='91Mbs'><tr id='91Mbs'><dt id='91Mbs'><q id='91Mbs'><span id='91Mbs'><b id='91Mbs'><form id='91Mbs'><ins id='91Mbs'></ins><ul id='91Mbs'></ul><sub id='91Mbs'></sub></form><legend id='91Mbs'></legend><bdo id='91Mbs'><pre id='91Mbs'><center id='91Mbs'></center></pre></bdo></b><th id='91Mbs'></th></span></q></dt></tr></i><div id='91Mbs'><tfoot id='91Mbs'></tfoot><dl id='91Mbs'><fieldset id='91Mbs'></fieldset></dl></div>

                        <small id='91Mbs'></small><noframes id='91Mbs'>