IOS 超级无敌ios基础

iOS 图片颜色选择器

2017-06-07  本文已影响197人  一只搬运工的自我修养

先看效果把

重写了图片touche事件

主要是拖动范围的设置

- (void)touchesBegan:(NSSet*)touches withEvent:(nullable UIEvent *)event {

UITouch *touch = [touches anyObject];

CGPoint pointL = [touch locationInView:self];

if (pow(pointL.x - self.bounds.size.width/2, 2)+pow(pointL.y-self.bounds.size.width/2, 2) <= pow(self.bounds.size.width/2, 2)) {

UIColor *color = [self colorAtPixel:pointL];

self.panView.center = pointL;

self.panView.backgroundColor =color;

if (self.currentColorBlock) {

self.currentColorBlock(color);

}}}



- (void)touchesMoved:(NSSet*)touches withEvent:(nullable UIEvent *)event {

UITouch *touch = [touches anyObject];

CGPoint pointL = [touch locationInView:self];

if (pow(pointL.x - self.bounds.size.width/2, 2)+pow(pointL.y-self.bounds.size.width/2, 2) <= pow(self.bounds.size.width/2, 2)) {

UIColor *color = [self getPointColorWithImage:self.image location:pointL];

self.panView.center = pointL;

self.panView.backgroundColor =color;

if (self.currentColorBlock) {

self.currentColorBlock(color);}}}

- (void)touchesEnded:(NSSet*)touches withEvent:(nullable UIEvent *)event {

UITouch *touch = [touches anyObject];

CGPoint pointL = [touch locationInView:self];

if (pow(pointL.x - self.bounds.size.width/2, 2)+pow(pointL.y-self.bounds.size.width/2, 2) <= pow(self.bounds.size.width/2, 2)) {

UIColor *color = [self getPointColorWithImage:self.image location:pointL];

self.panView.center = pointL;

self.panView.backgroundColor =color;

if (self.currentColorBlock) {

self.currentColorBlock(color);

}}}

//获取图片上某坐标点对应的像素的rgba值

- (UIColor *)getPointColorWithImage:(UIImage *)image location:(CGPoint)point{

UIColor *pointColor = nil;

//如果图片上不存在该点返回nil

if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, image.size.width, image.size.height), point)) {

return nil;

}

NSInteger pointX = truncl(point.x); //直接舍去小数,如1.58 -> 1.0

NSInteger pointY= truncl(point.y);

CGImageRef cgImage = image.CGImage;

NSUInteger width = image.size.width;
NSUInteger height = image.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();  //bitmap上下文使用的颜色空间
int bytesPerPixel = 4;  //bitmap在内存中所占的比特数
int bytesPerRow = bytesPerPixel * 1;  //bitmap的每一行在内存所占的比特数
NSUInteger bitsPerComponent = 8;  //内存中像素的每个组件的位数.例如,对于32位像素格式和RGB 颜色空间,你应该将这个值设为8.
unsigned char pixelData[4] = {0, 0, 0, 0};  //初始化像素信息
//创建位图文件环境。位图文件可自行百度 bitmap
CGContextRef context = CGBitmapContextCreate(pixelData,
1,
1,
bitsPerComponent,
bytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); //指定bitmap是否包含alpha通道,像素中alpha通道的相对位置,像素组件是整形还是浮点型等信息的字符串。
CGColorSpaceRelease(colorSpace);
CGContextSetBlendMode(context, kCGBlendModeCopy); //当一个颜色覆盖上另外一个颜色,两个颜色的混合方式
CGContextTranslateCTM(context, -pointX, pointY - (CGFloat)height);  //改变画布位置
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height ), cgImage);  //绘制图片
CGContextRelease(context);
CGFloat red = (CGFloat)pixelData[0] / 255.0f;
CGFloat green = (CGFloat)pixelData[1] / 255.0f;
CGFloat blue = (CGFloat)pixelData[2] / 255.0f;
CGFloat alpha = (CGFloat)pixelData[3] / 255.0f;
pointColor = [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
return pointColor;}

- (void)setImage:(UIImage *)image {

UIImage *temp = [self imageForResizeWithImage:image resize:CGSizeMake(self.frame.size.width, self.frame.size.width)];

[super setImage:temp];

}

- (UIImage *)imageForResizeWithImage:(UIImage *)picture resize:(CGSize)resize {

CGSize imageSize = resize; //CGSizeMake(25, 25)

UIGraphicsBeginImageContextWithOptions(imageSize, NO,0.0);

CGRect imageRect = CGRectMake(0.0, 0.0, imageSize.width, imageSize.height);

[picture drawInRect:imageRect];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

return image;

}

百度网盘链接: https://pan.baidu.com/s/1bpq1y4v 密码: bk9p

感谢作者:GitHub: https://github.com/Zws-China/WSColorPicker

上一篇下一篇

猜你喜欢

热点阅读