您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 教學 >> iOS 相關教學

iOS 從背景圖片取色的完整實作

編輯:iOS 相關教學

取色功能在許多 App 中相當實用,例如根據圖片自動調整主題色、製作取色器工具等。本文分享一段 iOS 從背景圖中取色的實作程式碼,透過解析圖片的像素資料,取得指定座標的 RGB 顏色。

實作原理

取色的核心在於把圖片還原成一張「像素陣列」:先建立一個 RGBA 的點陣圖上下文(bitmap context),把圖片繪製進去,再讀取其背後的原始位元資料。由於每個像素佔 4 個位元組(R、G、B、A 各 1 個),就能透過座標計算出對應的索引位置,進而取出顏色。

實現代碼

void *bitmapData; //內存空間的指針,大小等於圖像使用RGB通道所占用的字節數
static CGContextRef CreateRGBABitmapContext (CGImageRef inImage) {
    CGContextRef context = NULL;
    CGColorSpaceRef colorSpace;
    int bitmapByteCount;
    int bitmapBytesPerRow;
    size_t pixelsWide = CGImageGetWidth(inImage);   //獲取橫向的像素點個數
    size_t pixelsHigh = CGImageGetHeight(inImage);
    bitmapBytesPerRow = (pixelsWide * 4);           //每行像素點占用的字節數,每個像素的ARGB四個通道各占8個bit
    bitmapByteCount = (bitmapBytesPerRow * pixelsHigh);
    colorSpace = CGColorSpaceCreateDeviceRGB();      //創建依賴於設備的RGB通道
    bitmapData = malloc( bitmapByteCount );          //分配足夠容納圖片字節數的內存空間
    context = CGBitmapContextCreate (bitmapData, pixelsWide, pixelsHigh, 8, bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
    CGColorSpaceRelease( colorSpace );
    return context;
}
// 返回一個指針,指向一個數組,數組中每四個元素都是圖像上的一個像素點的RGBA數值(0-255)
static unsigned char *RequestImagePixelData(UIImage *inImage) {
    CGImageRef img = [inImage CGImage];
    CGSize size = [inImage size];
    CGContextRef cgctx = CreateRGBABitmapContext(img);
    CGRect rect = {{0,0},{size.width, size.height}};
    CGContextDrawImage(cgctx, rect, img);            //將目標圖像繪制到指定的上下文
    unsigned char *data = CGBitmapContextGetData (cgctx);
    CGContextRelease(cgctx);
    return data;
}
// 設置背景原圖片,即取色所用的圖片
- (void)setSourceImage:(NSString *)sourceImage ImageWidth:(int)_width ImageHeight:(int)_height {
    UIImage *im = [UIImage imageNamed:sourceImage];
    UIImage *newImage;
    UIImageView *view = [[UIImageView alloc] initWithImage:im];
    view.frame = CGRectMake(0, 0, _width, _height);
    UIGraphicsBeginImageContext(CGSizeMake(_width, _height));
    [im drawInRect:CGRectMake(0, 0, _width, _height)];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    width = newImage.size.width;
    height = newImage.size.height;
    imgPixel = RequestImagePixelData(newImage);      //將解析背景圖為像素,供取色用
}
// 計算顏色
-(UIColor*)calColor:(CGPoint)aPoint {
    int i = 4 * width * round(aPoint.y+imageView.frame.size.height/2) + 4 * round(aPoint.x+imageView.frame.size.width/2);
    int _r = (unsigned char)imgPixel[i];
    int _g = (unsigned char)imgPixel[i+1];
    int _b = (unsigned char)imgPixel[i+2];
    return [UIColor colorWithRed:_r/255.0f green:_g/255.0f blue:_b/255.0f alpha:1.0];
}
- (void)changColor:(UIColor *)color{
    int width_;
    if (![Util isIpad]) { width_ = 30; } else { width_ = 70; }
    UIGraphicsBeginImageContext(CGSizeMake(width_, width_));
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(ctx, 20, 20);
    CGContextSetFillColorWithColor(ctx, color.CGColor);
    if (![Util isIpad]) {
        CGContextAddArc(ctx, width_/2, width_/2, 14.5, 0, 6.3, 0);
    } else {
        CGContextAddArc(ctx, width_/2+0.5, width_/2, 31.3, 0, 6.3, 0);
    }
    CGContextFillPath(ctx);
    self->pickedColorImageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

重點說明

關鍵在於 calColor: 中的索引計算:因為每個像素佔 4 個位元組,所以要用「4 × 寬度 × y + 4 × x」算出該像素在陣列中的起始位置,再依序取出 R、G、B 三個通道並正規化到 0~1 的範圍,最後組合成 UIColor。掌握這個原理後,就能靈活應用在各種取色或色彩分析的需求上。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved