3 回答

TA貢獻1798條經(jīng)驗 獲得超3個贊
如果你想要的只是單個點的alpha值,你只需要一個只有alpha的單點緩沖區(qū)。我相信這應該足夠了:
// assume im is a UIImage, point is the CGPoint to test
CGImageRef cgim = im.CGImage;
unsigned char pixel[1] = {0};
CGContextRef context = CGBitmapContextCreate(pixel,
1, 1, 8, 1, NULL,
kCGImageAlphaOnly);
CGContextDrawImage(context, CGRectMake(-point.x,
-point.y,
CGImageGetWidth(cgim),
CGImageGetHeight(cgim)),
cgim);
CGContextRelease(context);
CGFloat alpha = pixel[0]/255.0;
BOOL transparent = alpha < 0.01;
如果不必每次都重新創(chuàng)建UIImage,這非常有效。
編輯2011年12月8日:
一位意見提供者指出,在某些情況下,圖像可能會被翻轉(zhuǎn)。我一直在考慮這個,我有點遺憾,我沒有直接使用UIImage編寫代碼,就像這樣(我認為原因是當時我不明白UIGraphicsPushContext):
// assume im is a UIImage, point is the CGPoint to test
unsigned char pixel[1] = {0};
CGContextRef context = CGBitmapContextCreate(pixel,
1, 1, 8, 1, NULL,
kCGImageAlphaOnly);
UIGraphicsPushContext(context);
[im drawAtPoint:CGPointMake(-point.x, -point.y)];
UIGraphicsPopContext();
CGContextRelease(context);
CGFloat alpha = pixel[0]/255.0;
BOOL transparent = alpha < 0.01;
我認為這樣可以解決翻轉(zhuǎn)問題。

TA貢獻1828條經(jīng)驗 獲得超3個贊
我是否需要翻譯UIKit和Core Graphics之間的坐標 - 即:y軸是倒置的嗎?
這是可能的。在CGImage中,像素數(shù)據(jù)采用英語閱讀順序:從左到右,從上到下。所以,陣列中的第一個像素是左上角; 第二個像素是從頂行左側開始的一個像素; 等等
假設你有這個權利,你還應該確保你在一個像素內(nèi)查看正確的組件。也許你期待RGBA但要求ARGB,反之亦然?;蛘?,也許您的字節(jié)順序錯誤(我不知道iPhone的字節(jié)順序是什么)。
或者我誤解了預乘alpha值?
聽起來不像。
對于那些不知道的人:預乘意味著顏色分量被alpha預乘; 無論顏色分量是否被預乘,alpha分量都是相同的。您可以通過將顏色分量除以alpha來反轉(zhuǎn)此(非預乘)。
- 3 回答
- 0 關注
- 855 瀏覽
添加回答
舉報