先看效果把
重寫了圖片touche事件
主要是拖動(dòng)范圍的設(shè)置
- (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);
}}}
//獲取圖片上某坐標(biāo)點(diǎn)對(duì)應(yīng)的像素的rgba值
- (UIColor *)getPointColorWithImage:(UIImage *)image location:(CGPoint)point{
UIColor *pointColor = nil;
//如果圖片上不存在該點(diǎn)返回nil
if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, image.size.width, image.size.height), point)) {
return nil;
}
NSInteger pointX = truncl(point.x); //直接舍去小數(shù),如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在內(nèi)存中所占的比特?cái)?shù)
int bytesPerRow = bytesPerPixel * 1;? //bitmap的每一行在內(nèi)存所占的比特?cái)?shù)
NSUInteger bitsPerComponent = 8;? //內(nèi)存中像素的每個(gè)組件的位數(shù).例如,對(duì)于32位像素格式和RGB 顏色空間谴轮,你應(yīng)該將這個(gè)值設(shè)為8.
unsigned char pixelData[4] = {0, 0, 0, 0};? //初始化像素信息
//創(chuàng)建位圖文件環(huán)境。位圖文件可自行百度 bitmap
CGContextRef context = CGBitmapContextCreate(pixelData,
1,
1,
bitsPerComponent,
bytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); //指定bitmap是否包含alpha通道黎比,像素中alpha通道的相對(duì)位置,像素組件是整形還是浮點(diǎn)型等信息的字符串鸳玩。
CGColorSpaceRelease(colorSpace);
CGContextSetBlendMode(context, kCGBlendModeCopy); //當(dāng)一個(gè)顏色覆蓋上另外一個(gè)顏色阅虫,兩個(gè)顏色的混合方式
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;
}
百度網(wǎng)盤鏈接: https://pan.baidu.com/s/1bpq1y4v 密碼: bk9p