第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

檢索UIImage的像素alpha值

檢索UIImage的像素alpha值

阿波羅的戰(zhàn)車 2019-08-02 16:18:07
檢索UIImage的像素alpha值我目前正在嘗試獲取UIImageView中像素的alpha值。我從[UIImageView image]中獲取了CGImage,并從中創(chuàng)建了一個RGBA字節(jié)數(shù)組。Alpha是預乘的。CGImageRef image = uiImage.CGImage;NSUInteger width = CGImageGetWidth(image);NSUInteger height = CGImageGetHeight(image);CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();rawData = malloc(height * width * 4);bytesPerPixel = 4;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), image);CGContextRelease(context);然后,我使用UIImageView中的坐標計算給定alpha通道的數(shù)組索引。int byteIndex = (bytesPerRow * uiViewPoint.y) + uiViewPoint.x * bytesPerPixel;unsigned char alpha = rawData[byteIndex + 3];但是我沒有得到我期望的價值。對于圖像的完全黑色透明區(qū)域,我得到alpha通道的非零值。我是否需要翻譯UIKit和Core Graphics之間的坐標 - 即:y軸是倒置的嗎?或者我誤解了預乘alpha值?更新:@Nikolai Ruhe的建議是關鍵。我實際上并不需要在UIKit坐標和Core Graphics坐標之間進行轉(zhuǎn)換。但是,在設置混合模式后,我的alpha值就是我的預期:CGContextSetBlendMode(context, kCGBlendModeCopy);
查看完整描述

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)問題。


查看完整回答
反對 回復 2019-08-02
?
倚天杖

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)此(非預乘)。


查看完整回答
反對 回復 2019-08-02
  • 3 回答
  • 0 關注
  • 855 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號