[Swift]iOS開發(fā)獲取圖像RGB值超詳解

在一開始拿到這個項目的時候, 我是覺得很輕松的,以前做工PHP的驗證碼生成和自動識別項目,很輕松完成惦费。想必是當(dāng)時的php庫比較的完善吧趟脂,也有可能當(dāng)時涉及的比較淺泰讽,壓根沒有機會去觸及較深層的知識。這次花了好幾天的時間去解決一個問題還是沒解決掉昔期,讓我感觸頗深已卸。在國內(nèi)論壇發(fā)帖也沒有人回,最后還是在stackoverflow(問題鏈接)上發(fā)帖有兩個大神一語道破天機硼一,還附上了apple開發(fā)者檔案的解決方法累澡,才讓度過了一個難關(guān)。特此把自己覺得有用的東西寫在這里般贼,大家一起共勉愧哟。

問題1

用Photoshop生成的一張純紅色圖,設(shè)置的顏色是(238,42,43)但是到最后按Apple開發(fā)者文檔提到的方法取到的顏色是(231, 17,33), 查了很多東西哼蛆,應(yīng)該都沒有錯蕊梧,于是繼續(xù)厚著臉皮求教大神。
大神回復(fù)

Re the color issue, I just created an image in Photoshop with 238, 42, 43 and when I processed it like shown above, in my app I saw 238, 42, 43. I suspect that there's some colorspace inconsistency or something like that. I'd first open up the JPG again, and confirm that the values weren't altered in the export process or something like that.

所以我就按大神的方法用別的軟件再一次取了色看用photoshop生成的圖片的RGB值腮介。結(jié)果肥矢!結(jié)果!真的如大神所言萤厅,圖像倒出來顏色就變了橄抹。有問題的是圖像本身,而不是程序惕味!
另一大神直接給了一個鏈接

Take a look at this answer and the linked article.

這的是獲益匪淺啊??楼誓!

問題2

為什么之前獲取RGB的方法會發(fā)生錯誤?

之前取色錯誤的圖片的 BitmapInfo.RawValue 是 8194 也就是
CGBitmapInfo.ByteOrder32Little.RawValue | CGImageAlphaInfo.PremultipliedFirst.rawValue
而正確的圖片的BitmapInfo.RawValue 是 5
也就是說不同的BitmapInfo導(dǎo)致了顏色提取的錯誤
Apple開發(fā)者文檔中關(guān)于 BitmapInfo 內(nèi)容中文翻譯

字節(jié)序的相關(guān)博客
Big Endian 和 Little Endian

那么最關(guān)心的問題來了 該怎么解決呢名挥?

Apple 開發(fā)者文檔QA的OC代碼

void ManipulateImagePixelData(CGImageRef inImage){ 
// Create the bitmap context CGContextRef cgctx = CreateARGBBitmapContext(inImage);
  if (cgctx == NULL) { 
  // error creating context
   return; 
  } 
  // Get image width, height. We'll use the entire image. 
  size_t w = CGImageGetWidth(inImage); 
  size_t h = CGImageGetHeight(inImage); 
  CGRect rect = {{0,0},{w,h}}; 
  // Draw the image to the bitmap context. Once we draw, the memd dory            

// allocated for the context for rendering will then contain the
// raw image data in the specified color space. CGContextDrawImage(cgctx, rect, inImage); // Now we can get a pointer to the image data associated with the bitmap // context. void *data = CGBitmapContextGetData (cgctx); if (data != NULL) { // **** You have a pointer to the image data **** // **** Do stuff with the data here **** } // When finished, release the context CGContextRelease(cgctx); // Free image data memory for the context if (data) { free(data); }}CGContextRef CreateARGBBitmapContext (CGImageRef inImage){ CGContextRef context = NULL; CGColorSpaceRef colorSpace; void * bitmapData; int bitmapByteCount; int bitmapBytesPerRow; // Get image width, height. We'll use the entire image. size_t pixelsWide = CGImageGetWidth(inImage); size_t pixelsHigh = CGImageGetHeight(inImage); // Declare the number of bytes per row. Each pixel in the bitmap in this // example is represented by 4 bytes; 8 bits each of red, green, blue, and // alpha. bitmapBytesPerRow = (pixelsWide * 4); bitmapByteCount = (bitmapBytesPerRow * pixelsHigh); // Use the generic RGB color space. colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); if (colorSpace == NULL) { fprintf(stderr, "Error allocating color space\n"); return NULL; } // Allocate memory for image data. This is the destination in memory // where any drawing to the bitmap context will be rendered. bitmapData = malloc( bitmapByteCount ); if (bitmapData == NULL) { fprintf (stderr, "Memory not allocated!"); CGColorSpaceRelease( colorSpace ); return NULL; } // Create the bitmap context. We want pre-multiplied ARGB, 8-bits // per component. Regardless of what the source image format is // (CMYK, Grayscale, and so on) it will be converted over to the format // specified here by CGBitmapContextCreate. context = CGBitmapContextCreate (bitmapData, pixelsWide, pixelsHigh, 8, // bits per component bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst); if (context == NULL) { free (bitmapData); fprintf (stderr, "Context not created!"); } // Make sure and release colorspace before returning CGColorSpaceRelease( colorSpace ); return context;}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末疟羹,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子禀倔,更是在濱河造成了極大的恐慌榄融,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,640評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件救湖,死亡現(xiàn)場離奇詭異愧杯,居然都是意外死亡,警方通過查閱死者的電腦和手機鞋既,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,254評論 3 395
  • 文/潘曉璐 我一進店門力九,熙熙樓的掌柜王于貴愁眉苦臉地迎上來耍铜,“玉大人,你說我怎么就攤上這事跌前∽丶妫” “怎么了?”我有些...
    開封第一講書人閱讀 165,011評論 0 355
  • 文/不壞的土叔 我叫張陵抵乓,是天一觀的道長伴挚。 經(jīng)常有香客問我,道長灾炭,這世上最難降的妖魔是什么茎芋? 我笑而不...
    開封第一講書人閱讀 58,755評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮咆贬,結(jié)果婚禮上败徊,老公的妹妹穿的比我還像新娘帚呼。我一直安慰自己掏缎,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,774評論 6 392
  • 文/花漫 我一把揭開白布煤杀。 她就那樣靜靜地躺著眷蜈,像睡著了一般。 火紅的嫁衣襯著肌膚如雪沈自。 梳的紋絲不亂的頭發(fā)上酌儒,一...
    開封第一講書人閱讀 51,610評論 1 305
  • 那天,我揣著相機與錄音枯途,去河邊找鬼忌怎。 笑死,一個胖子當(dāng)著我的面吹牛酪夷,可吹牛的內(nèi)容都是我干的榴啸。 我是一名探鬼主播,決...
    沈念sama閱讀 40,352評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼晚岭,長吁一口氣:“原來是場噩夢啊……” “哼鸥印!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起坦报,我...
    開封第一講書人閱讀 39,257評論 0 276
  • 序言:老撾萬榮一對情侶失蹤库说,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后片择,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體潜的,經(jīng)...
    沈念sama閱讀 45,717評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,894評論 3 336
  • 正文 我和宋清朗相戀三年字管,在試婚紗的時候發(fā)現(xiàn)自己被綠了啰挪。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片疏咐。...
    茶點故事閱讀 40,021評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖脐供,靈堂內(nèi)的尸體忽然破棺而出浑塞,到底是詐尸還是另有隱情,我是刑警寧澤政己,帶...
    沈念sama閱讀 35,735評論 5 346
  • 正文 年R本政府宣布酌壕,位于F島的核電站,受9級特大地震影響歇由,放射性物質(zhì)發(fā)生泄漏卵牍。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,354評論 3 330
  • 文/蒙蒙 一沦泌、第九天 我趴在偏房一處隱蔽的房頂上張望糊昙。 院中可真熱鬧,春花似錦谢谦、人聲如沸释牺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,936評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽没咙。三九已至,卻和暖如春千劈,著一層夾襖步出監(jiān)牢的瞬間祭刚,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,054評論 1 270
  • 我被黑心中介騙來泰國打工墙牌, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留涡驮,地道東北人。 一個月前我還...
    沈念sama閱讀 48,224評論 3 371
  • 正文 我出身青樓喜滨,卻偏偏與公主長得像捉捅,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子鸿市,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,974評論 2 355

推薦閱讀更多精彩內(nèi)容