#pragma mark - 获取图片某点的颜色
- (void)colorAtPixel:(CGPoint)point {
UIImageView*imageView =self.icon;
NSIntegerpointX =trunc(point.x);
NSIntegerpointY =trunc(point.y);
CGImageRefcgImage = imageView.image.CGImage;
NSUIntegerwidth = imageView.bounds.size.width;
NSUIntegerheight = imageView.bounds.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
intbytesPerPixel =4;
intbytesPerRow = bytesPerPixel *1;
NSUIntegerbitsPerComponent =8;
unsignedcharpixelData[4] = {0,0,0,0};
CGContextRefcontext =CGBitmapContextCreate(pixelData,
1,
1,
bitsPerComponent,
bytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast|kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextSetBlendMode(context, kCGBlendModeCopy);
// Draw the pixel we are interested in onto the bitmap context
CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height);
CGContextDrawImage(context,CGRectMake(0.0f,0.0f, (CGFloat)width, (CGFloat)height), cgImage);
CGContextRelease(context);
// 把[0,255]的颜色值映射至[0,1]区间
CGFloatred = (CGFloat)pixelData[0];
CGFloatgreen = (CGFloat)pixelData[1];
CGFloatblue = (CGFloat)pixelData[2];
CGFloatalpha = (CGFloat)pixelData[3];
// 改变输入框
self.fieldR.text= [NSStringstringWithFormat:@"%.0f", red];
self.fieldG.text= [NSStringstringWithFormat:@"%.0f", green];
self.fieldB.text= [NSStringstringWithFormat:@"%.0f", blue];
self.fieldA.text= [NSStringstringWithFormat:@"%.0f", alpha];
// 改变滑动条
self.sliderR.value= red;
self.sliderG.value= green;
self.sliderB.value= blue;
self.sliderA.value= alpha;
[self updateUIAction];
}