国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

iOS顏色攝合器,獲取圖片某點的顏色值

2019-11-07 23:52:45
字體:
來源:轉載
供稿:網友

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 会昌县| 文山县| 罗定市| 峨眉山市| 藁城市| 横峰县| 绥芬河市| 西乌珠穆沁旗| 东乡族自治县| 石景山区| 湖南省| 吉木乃县| 阳西县| 松桃| 城步| 朔州市| 恩施市| 来凤县| 遂昌县| 东港市| 上栗县| 天镇县| 新源县| 辰溪县| 寿宁县| 五家渠市| 太湖县| 洮南市| 保山市| 泽普县| 宁河县| 平顺县| 中卫市| 利津县| 阳春市| 龙泉市| 历史| 高邑县| 郑州市| 开原市| 八宿县|