iOS:基于圖片像素點修改圖片顏色

思考:圖片是由大量的小方格組成的可見的圖像赏酥,每一個小方格存儲a、r谆构、g裸扶、b用來定義顏色,所以寫了一個demo搬素,能夠獲取一張圖片的所有像素點顏色并能進行修改呵晨。

效果如下:


Untitled.gif
//獲取圖片所有像素點顏色
+ (NSMutableArray<UIColor*> *)getPixelColorsWithImage:(UIImage*)image
{
    UInt32 * inputPixels;
    
    // 分配內(nèi)存
    CGImageRef inputCGImage = [image CGImage];
    NSUInteger imageWidth = CGImageGetWidth(inputCGImage);
    NSUInteger imageHeight = CGImageGetHeight(inputCGImage);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    NSUInteger bytesPerPixel = 4;
    NSUInteger bitsPerComponent = 8;
    
    NSUInteger inputBytesPerRow = bytesPerPixel * imageWidth;//每行字節(jié)數(shù)
    
    inputPixels = (UInt32 *)calloc(imageHeight * imageWidth, sizeof(UInt32));//內(nèi)存空間首個像素
    
    // 創(chuàng)建context
    CGContextRef context = CGBitmapContextCreate(inputPixels, imageWidth, imageHeight, bitsPerComponent, inputBytesPerRow, colorSpace,kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), image.CGImage);
    
    NSMutableArray* arr = [NSMutableArray array];
    // 遍歷像素
    for (NSUInteger j = 0; j < imageHeight; j++) {
        for (NSUInteger i = 0; i < imageWidth; i++) {
            UInt32 * currentPixel = inputPixels + (j * imageWidth) + i;
            UInt32 color = *currentPixel;
            UInt32 red,green,blue,alpha;
            red=R(color);green=G(color);blue=B(color);alpha=A(color);
            [arr addObject:[UIColor colorWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:alpha/255.0]];
        }
    }
    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);
    free(inputPixels);
    return arr;
}
//更改圖片像素點顏色
+ (UIImage*)changePixelColorsWithImage:(UIImage*)image colors:(NSArray<UIColor*>*)colors toColor:(UIColor*)toColor
{
    UInt32 * inputPixels;
    
    // 分配內(nèi)存
    CGImageRef inputCGImage = [image CGImage];
    NSUInteger imageWidth = CGImageGetWidth(inputCGImage);
    NSUInteger imageHeight = CGImageGetHeight(inputCGImage);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    NSUInteger bytesPerPixel = 4;
    NSUInteger bitsPerComponent = 8;
    
    NSUInteger inputBytesPerRow = bytesPerPixel * imageWidth;//每行字節(jié)數(shù)
    
    inputPixels = (UInt32 *)calloc(imageHeight * imageWidth, sizeof(UInt32));//內(nèi)存空間首個像素
    
    // 創(chuàng)建context
    CGContextRef context = CGBitmapContextCreate(inputPixels, imageWidth, imageHeight, bitsPerComponent, inputBytesPerRow, colorSpace,kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), image.CGImage);
    
    // 遍歷像素
    for (NSUInteger j = 0; j < imageHeight; j++) {
        for (NSUInteger i = 0; i < imageWidth; i++) {
            UInt32 * currentPixel = inputPixels + (j * imageWidth) + i;
            UInt32 color = *currentPixel;
            UInt32 br = 0,red,green,blue,alpha;
            red=R(color); green=G(color); blue=B(color); alpha=A(color);
            
            [colors enumerateObjectsUsingBlock:^(UIColor* newColor, NSUInteger idx, BOOL * _Nonnull stop) {
                const CGFloat *components = CGColorGetComponents(newColor.CGColor);
                if(red == components[0] * 255.0 && green == components[1] * 255.0 && blue == components[2] * 255.0){
                    const CGFloat *components = CGColorGetComponents(toColor.CGColor);
                    int red = components[0] * 255.0;
                    int green = components[1] * 255.0;
                    int blue = components[2] * 255.0;
                    int alpha = components[3] * 255.0;
                    *currentPixel = RGBAMake(red,green,blue, alpha);
                    *stop = YES;
                    return ;
                }
            }];
        }
    }
        
    //轉(zhuǎn)換UIImage
    CGImageRef newCGImage = CGBitmapContextCreateImage(context);
    UIImage * newImage = [UIImage imageWithCGImage:newCGImage];
    
    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);
    free(inputPixels);
    
    return newImage;
}

GitHub:https://github.com/qw9685/changePixelColor/tree/master

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市熬尺,隨后出現(xiàn)的幾起案子摸屠,更是在濱河造成了極大的恐慌,老刑警劉巖猪杭,帶你破解...
    沈念sama閱讀 216,372評論 6 498
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件餐塘,死亡現(xiàn)場離奇詭異,居然都是意外死亡皂吮,警方通過查閱死者的電腦和手機戒傻,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,368評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蜂筹,“玉大人需纳,你說我怎么就攤上這事∫张玻” “怎么了不翩?”我有些...
    開封第一講書人閱讀 162,415評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長麻裳。 經(jīng)常有香客問我口蝠,道長,這世上最難降的妖魔是什么津坑? 我笑而不...
    開封第一講書人閱讀 58,157評論 1 292
  • 正文 為了忘掉前任妙蔗,我火速辦了婚禮,結(jié)果婚禮上疆瑰,老公的妹妹穿的比我還像新娘眉反。我一直安慰自己,他們只是感情好穆役,可當我...
    茶點故事閱讀 67,171評論 6 388
  • 文/花漫 我一把揭開白布寸五。 她就那樣靜靜地躺著,像睡著了一般耿币。 火紅的嫁衣襯著肌膚如雪梳杏。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,125評論 1 297
  • 那天,我揣著相機與錄音秘狞,去河邊找鬼叭莫。 笑死,一個胖子當著我的面吹牛烁试,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播拢肆,決...
    沈念sama閱讀 40,028評論 3 417
  • 文/蒼蘭香墨 我猛地睜開眼减响,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了郭怪?” 一聲冷哼從身側(cè)響起支示,我...
    開封第一講書人閱讀 38,887評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎鄙才,沒想到半個月后颂鸿,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,310評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡攒庵,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,533評論 2 332
  • 正文 我和宋清朗相戀三年嘴纺,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片浓冒。...
    茶點故事閱讀 39,690評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡栽渴,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出稳懒,到底是詐尸還是另有隱情闲擦,我是刑警寧澤,帶...
    沈念sama閱讀 35,411評論 5 343
  • 正文 年R本政府宣布场梆,位于F島的核電站墅冷,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏或油。R本人自食惡果不足惜寞忿,卻給世界環(huán)境...
    茶點故事閱讀 41,004評論 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望装哆。 院中可真熱鬧罐脊,春花似錦、人聲如沸蜕琴。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,659評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽凌简。三九已至上炎,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背藕施。 一陣腳步聲響...
    開封第一講書人閱讀 32,812評論 1 268
  • 我被黑心中介騙來泰國打工寇损, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人裳食。 一個月前我還...
    沈念sama閱讀 47,693評論 2 368
  • 正文 我出身青樓矛市,卻偏偏與公主長得像,于是被迫代替她去往敵國和親诲祸。 傳聞我的和親對象是個殘疾皇子浊吏,可洞房花燭夜當晚...
    茶點故事閱讀 44,577評論 2 353