iOS顏色攝合器,獲取圖片某點的顏色值
版權聲明:本文為博主原創文章
1.新建一個繼承于UIImageView的類-YWColorByImageView 2.重寫set方法,并通過上下圖文創建實際對應像素的image
/** 重設image @param image image */- (void)setImage:(UIImage *)image { UIImage *temp = [self imageForResizeWithImage:image resize:CGSizeMake(self.frame.size.width, self.frame.size.width)]; [super setImage:temp];}/** 通過上下圖文創建self對應像素的image @param picture 要加載的圖片 @param resize 實際self的大小 @return 返回self對應像素的大小的image */- (UIImage *)imageForResizeWithImage:(UIImage *)picture resize:(CGSize)resize { CGSize imageSize = resize; //CGSizeMake(25, 25) /** UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale); 1.參數size為新創建的位圖上下文的大小 2.opaque—透明開關,如果圖形完全不用透明,設置為YES以優化位圖的存儲 3.scale—–縮放因子 iphone 4是2.0,其他是1.0;實際上設為0后,系統就會自動設置正確的比例了。 */ UIGraphicsBeginImageContextWithOptions(imageSize, NO,0.0); CGRect imageRect = CGRectMake(0.0, 0.0, imageSize.width, imageSize.height); [picture drawInRect:imageRect]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); return image;}通過觸摸點,判斷該點是否在范圍內,然后獲取某點的顏色值- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location_point = [touch locationInView:self]; if ([YWDistanceTool getlayer:location_point withRadius:self.bounds.size.width/2]) { UIColor *color = [UIColor colorAtPixel:location_point withImage:self.image]; if (self.pointColorBlock) { self.pointColorBlock(color); } if ([self.delegate respondsToSelector:@selector(getCurrentColor:)]) { [self.delegate getCurrentColor:color]; } }}- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location_point = [touch locationInView:self]; if ([YWDistanceTool getlayer:location_point withRadius:self.bounds.size.width/2]) { UIColor *color = [UIColor colorAtPixel:location_point withImage:self.image]; if (self.pointColorBlock) { self.pointColorBlock(color); } if ([self.delegate respondsToSelector:@selector(getCurrentColor:)]) { [self.delegate getCurrentColor:color]; } }}- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint location_point = [touch locationInView:self]; if ([YWDistanceTool getlayer:location_point withRadius:self.bounds.size.width/2]) { UIColor *color = [UIColor colorAtPixel:location_point withImage:self.image]; if (self.pointColorBlock) { self.pointColorBlock(color); } if ([self.delegate respondsToSelector:@selector(getCurrentColor:)]) { [self.delegate getCurrentColor:color]; } }}/** 獲取圖片某一點的顏色 @param point 點擊圖片的某一點 @param image 圖片 @return 圖片某點的顏色值 */+ (UIColor *)colorAtPixel:(CGPoint)point withImage:(UIImage *)image { // 判斷給定的點是否被一個CGRect包含 if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, image.size.width, image.size.height), point)) { return nil; } // trunc(n1,n2),n1表示被截斷的數字,n2表示要截斷到那一位。n2可以是負數,表示截斷小數點前。注意,TRUNC截斷不是四舍五入。 // TRUNC(15.79)---15 // trunc(15.79,1)--15.7 NSInteger pointX = trunc(point.x); NSInteger pointY = trunc(point.y); CGImageRef cgImage = image.CGImage; NSUInteger width = image.size.width; NSUInteger height = image.size.height; // 創建色彩標準 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); int bytesPerPixel = 4; int bytesPerRow = bytesPerPixel * 1; NSUInteger bitsPerComponent = 8; unsigned char pixelData[4] = { 0, 0, 0, 0 }; CGContextRef context = CGBitmapContextCreate(pixelData, 1, 1, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPRemultipliedLast | kCGBitmapByteOrder32Big); 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; return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];}源碼請轉:https://github.com/flyOfYW/YWDemo.git
新聞熱點
疑難解答