2017-08-18 69 views
-1

我想從屏幕上的協調中獲取顏色並將其放置在變量中。我正在使用Theos,並且只是在我的iOS 5設備上進行學習,所以請不要開始「停止浪費時間對舊iOS進行調整」,正如我在所有帖子中看到的那樣:從Objective-C中的像素獲取顏色

+0

後一些代碼,你試過嗎?沒有人會餵你的勺子... – Stefan

+0

好吧,我沒有做任何事情,因爲我必須先得到顏色 – Saeed

+1

答案已經可以在這裏 - > https://stackoverflow.com/questions/4616778/ios-detect-the- -一個像素顏色的 –

回答

0

試試下面的代碼:

// 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; 
} 

你可以在這裏找到這些文件:https://github.com/ivanzoid/ikit/tree/master/UIView%2BColorOfPoint

而且,這裏覈對答案的詳細信息:How to get the color of a pixel in an UIView?