ios - 在 UIImageView 中访问单个像素 alpha
<p><p>我正在尝试检测碰撞忽略透明区域。我已经完成了所有这些,但是返回 UIImageView 中单个像素的函数是否透明。目前我从这里整理了一些帖子:</p>
<pre><code>- (BOOL)IsPixelNonTransparentFromImage:(UIImage*)image atX:(int)x andY:(int)y {
CGImageRef imageRef = ;
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
NSUInteger byteIndex = (bytesPerRow * y) + x * bytesPerPixel;
CGFloat alpha = (rawData * 1.0)/255.0;
free(rawData);
return alpha > 0.0;
}
</code></pre>
<p>这不仅对我不起作用,而且我很确定有一个更时尚的解决方案可以只找到一个像素。任何帮助将不胜感激。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>下面是检测单个像素 alpha 的代码:</p>
<pre><code>- (BOOL)IsPixelNonTransparentFromImage:(UIImage*)image atX:(int)x andY:(int)y {
unsigned char pixel = { 0 };
CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 1, NULL, (CGBitmapInfo)kCGImageAlphaOnly);
UIGraphicsPushContext(context);
;
UIGraphicsPopContext();
CGContextRelease(context);
return pixel != 0;
}
</code></pre>
<p>想法是把图像绘制到一个一个像素一个像素的上下文中,在<code>(-x, -y)</code>的偏移处,这样<code>(x, y)</code> 被“绘制”到我们创建的位图上下文的单个像素中。这样可以避免动态分配和渲染整个图片以加快处理速度。 <code>kCGImageAlphaOnly</code>用于只截取图片的alpha channel 。</p>
<p>这是一个测试这段代码的程序:</p>
<pre><code>UIImage *image = ;
NSLog(@"%@", );
for (int y = 0 ; y < image.size.height ; y++) {
NSMutableString *buf = ;
for (int x = 0 ; x < image.size.width ; x++) {
? '*' : '-'];
}
NSLog(@"%@", buf);
}
</code></pre>
<p>这是一个 <a href="http://s15.postimg.org/nppqftfg7/Blue_Circle.png" rel="noreferrer noopener nofollow">link to the <code>BlueCircle.png</code> image that I used for testing</a> 。</p>
<p>当这段代码运行时,我在日志中得到这个打印输出:</p>
<pre><code>2015-04-15 20:15:55.213 ---------************---------
2015-04-15 20:15:55.213 -------****************-------
2015-04-15 20:15:55.214 ------******************------
2015-04-15 20:15:55.214 ----**********************----
2015-04-15 20:15:55.215 ---************************---
2015-04-15 20:15:55.216 ---************************---
2015-04-15 20:15:55.216 --**************************--
2015-04-15 20:15:55.217 -****************************-
2015-04-15 20:15:55.217 -****************************-
2015-04-15 20:15:55.218 ******************************
2015-04-15 20:15:55.219 ******************************
2015-04-15 20:15:55.219 ******************************
2015-04-15 20:15:55.220 ******************************
2015-04-15 20:15:55.278 ******************************
2015-04-15 20:15:55.279 ******************************
2015-04-15 20:15:55.279 ******************************
2015-04-15 20:15:55.280 ******************************
2015-04-15 20:15:55.280 ******************************
2015-04-15 20:15:55.281 ******************************
2015-04-15 20:15:55.281 ******************************
2015-04-15 20:15:55.282 ******************************
2015-04-15 20:15:55.282 -****************************-
2015-04-15 20:15:55.283 -****************************-
2015-04-15 20:15:55.283 --**************************--
2015-04-15 20:15:55.284 ---************************---
2015-04-15 20:15:55.284 ---************************---
2015-04-15 20:15:55.330 ----**********************----
2015-04-15 20:15:55.331 ------******************------
2015-04-15 20:15:55.331 -------****************-------
2015-04-15 20:15:55.332 ---------************---------
</code></pre></p>
<p style="font-size: 20px;">关于ios - 在 UIImageView 中访问单个像素 alpha,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/29660888/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/29660888/
</a>
</p>
页:
[1]