2009-07-21 34 views
27

我正在嘗試開發一個小應用程序。其中,我需要檢測UIView內像素的顏色。我有一個CGPoint定義我需要的像素。使用CoreAnimation更改UIView的顏色。如何獲取UIView中像素的顏色?

我知道有一些複雜的方式來提取UIImages顏色信息。但是我找不到UIViews的解決方案。

僞碼,我在尋找類似

pixel = [view getPixelAtPoint:myPoint]; 
UIColor *mycolor = [pixel getColor]; 

任何投入極大的讚賞。

+0

您是否找到(快速)解決方案? – CodeAndCats 2009-10-11 01:42:42

+0

使用位圖上下文的速度足以達到我的目的。試試看看它是如何爲你工作的。 – 0x90 2009-10-15 02:03:15

回答

8

這是很可怕的和緩慢的。基本上你創建你分配,以便可以讀出存儲器中的後備存儲器的位圖的上下文,則呈現在上下文的觀點層和讀取在RAM中的相應點。

如果你知道如何做到這一點的圖像已經可以做這樣的事情:

- (UIImage *)imageForView:(UIView *)view { 
    UIGraphicsBeginImageContext(view.frame.size); 
    [view.layer renderInContext: UIGraphicsGetCurrentContext()]; 
    UIImage *retval = UIGraphicsGetImageFromCurrentImageContext(void); 
    UIGraphicsEndImageContext(); 

    return retval; 
} 

,然後你會得到一個圖像,你可以得到的像素數據。我確信你處理圖像的機制涉及到將它們渲染到上下文中,所以你可以將它與這個合併,並將實際的圖像創建分解出來。所以,如果你需要,可以和刪除,您加載圖像和與上下文替換位渲染:

[view.layer renderInContext: UIGraphicsGetCurrentContext()]; 

你應該不錯。

3

修正了一些小錯誤

- (UIImage *)imageForView:(UIView *)view { 
    UIGraphicsBeginImageContext(view.frame.size); 
    [view.layer renderInContext: UIGraphicsGetCurrentContext()]; 
    UIImage *retval = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    return retval; 
} 

此外,添加

#import <QuartzCore/QuartzCore.h> 

到您的文件,以避免警告消息。

與代碼結合你的代碼中發現here完美的作品。

+0

爲什麼它看起來只是複製了Louis的代碼,並且在調用`UIGraphicsGetImageFromCurrentImageContext()`的過程中刪除了多餘的「void」? – 2010-10-28 04:58:06

+1

這就是我所做的。什麼是更好的方法來處理這裏的更正? – 0x90 2010-10-28 21:33:36

70

這是更有效的解決方案:

// UIView+ColorOfPoint.h 
@interface UIView (ColorOfPoint) 
- (UIColor *) colorOfPoint:(CGPoint)point; 
@end 

// UIView+ColorOfPoint.m 
#import "UIView+ColorOfPoint.h" 
#import <QuartzCore/QuartzCore.h> 

@implementation UIView (ColorOfPoint) 

- (UIColor *) colorOfPoint:(CGPoint)point 
{ 
    unsigned char pixel[4] = {0}; 

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

    CGContextRef context = CGBitmapContextCreate(pixel, 1, 1, 8, 4, colorSpace, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast); 

    CGContextTranslateCTM(context, -point.x, -point.y); 

    [self.layer renderInContext:context]; 

    CGContextRelease(context); 
    CGColorSpaceRelease(colorSpace); 

    //NSLog(@"pixel: %d %d %d %d", pixel[0], pixel[1], pixel[2], pixel[3]); 

    UIColor *color = [UIColor colorWithRed:pixel[0]/255.0 green:pixel[1]/255.0 blue:pixel[2]/255.0 alpha:pixel[3]/255.0]; 

    return color; 
} 

@end 

鏈接到文件:https://github.com/ivanzoid/ikit/tree/master/UIView+ColorOfPoint

14

一個swift'ified版本的ivanzoid的回答

斯威夫特3

extension CALayer { 

    func colorOfPoint(point:CGPoint) -> CGColor { 

     var pixel: [CUnsignedChar] = [0, 0, 0, 0] 

     let colorSpace = CGColorSpaceCreateDeviceRGB() 
     let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue) 

     let context = CGContext(data: &pixel, width: 1, height: 1, bitsPerComponent: 8, bytesPerRow: 4, space: colorSpace, bitmapInfo: bitmapInfo.rawValue) 

     context!.translateBy(x: -point.x, y: -point.y) 

     self.render(in: context!) 

     let red: CGFloat = CGFloat(pixel[0])/255.0 
     let green: CGFloat = CGFloat(pixel[1])/255.0 
     let blue: CGFloat = CGFloat(pixel[2])/255.0 
     let alpha: CGFloat = CGFloat(pixel[3])/255.0 

     let color = UIColor(red:red, green: green, blue:blue, alpha:alpha) 

     return color.cgColor 
    } 
} 

夫特2

extension CALayer { 

    func colorOfPoint(point:CGPoint)->CGColorRef 
    { 
     var pixel:[CUnsignedChar] = [0,0,0,0] 

     let colorSpace = CGColorSpaceCreateDeviceRGB() 
     let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue) 

     let context = CGBitmapContextCreate(&pixel, 1, 1, 8, 4, colorSpace, bitmapInfo.rawValue) 

     CGContextTranslateCTM(context, -point.x, -point.y) 

     self.renderInContext(context!) 

     let red:CGFloat = CGFloat(pixel[0])/255.0 
     let green:CGFloat = CGFloat(pixel[1])/255.0 
     let blue:CGFloat = CGFloat(pixel[2])/255.0 
     let alpha:CGFloat = CGFloat(pixel[3])/255.0 

     let color = UIColor(red:red, green: green, blue:blue, alpha:alpha) 

     return color.CGColor 
    } 

} 

基於底層CALayer周圍,但容易翻譯回UIView