所以我有一些原始圖像。使用蒙版獲取圖像像素
我需要使用特定的蒙版獲取並顯示此圖像的某些部分。蒙版不是矩形或形狀。它包含不同的多邊形和形狀。
是否有任何方法或教程如何實現?或從哪裏開始做到這一點?我應該寫一些小的着色器或比較圖像的適當像素或類似的東西?
對任何建議感到高興。
謝謝
所以我有一些原始圖像。使用蒙版獲取圖像像素
我需要使用特定的蒙版獲取並顯示此圖像的某些部分。蒙版不是矩形或形狀。它包含不同的多邊形和形狀。
是否有任何方法或教程如何實現?或從哪裏開始做到這一點?我應該寫一些小的着色器或比較圖像的適當像素或類似的東西?
對任何建議感到高興。
謝謝
你可以把你的形象在一個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);
謝謝你真的,它的顯示和獲取像素? – 2013-03-25 07:14:51
@George沒問題。我已經添加了一些示例代碼來從像這樣的設置中提取像素數據。 – 2013-03-25 07:41:51
我想你救了我的一天,甚至超過一天!! :) – 2013-03-25 08:25:32