- (UIColor *)colorAtPixel:(CGPoint)point {

// Cancel if point is outside image coordinates

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

return nil;

}

NSInteger pointX =
trunc(point.x);

NSInteger pointY =
trunc(point.y);

CGImageRef cgImage =
self.CGImage;

NSUInteger width =
self.size.width;

NSUInteger height =
self.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);

// 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);

// Convert color values [0..255] to floats [0.0..1.0]

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];

}


UIImage+ColorAtPixel.h

#import <UIKit/UIKit.h>

/*
A category on UIImage that enables you to query the color value of arbitrary
pixels of the image.
*/
@interface UIImage (ColorAtPixel) - (UIColor *)colorAtPixel:(CGPoint)point; @end #import <CoreGraphics/CoreGraphics.h> #import "UIImage+ColorAtPixel.h" @implementation UIImage (ColorAtPixel) - (UIColor *)colorAtPixel:(CGPoint)point {
// Cancel if point is outside image coordinates
if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.size.width, self.size.height), point)) {
return nil;
} NSInteger pointX = trunc(point.x);
NSInteger pointY = trunc(point.y);
CGImageRef cgImage = self.CGImage;
NSUInteger width = self.size.width;
NSUInteger height = self.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); // 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); // Convert color values [0..255] to floats [0.0..1.0]
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];
} @end

iOS 获取图片某一点的颜色对象(UIColor*)。的更多相关文章

  1. iOS获取图片的Base64String,兼容Android,java,web,jpg(jpeg),png

    呃呃呃……需求的来源又是同学,对!又是! 废话不哆嗦,怎么把一张图在iOS上转一个Base64String出来,稍微了解的,或者随便搜一下,都能搞定一大堆,但是!!!! 自己(iOS)转自己用,完全没 ...

  2. iOS获取图片格式

    我们系统使用的一般文件格式有png.jpg.jpeg.GIF.TIFF 等格式: 图片是我们开发过程中最常见的东西,一般每个公司都会有自己的文件服务器,客户端人员都会从文件服务器中获取相关的图片信息: ...

  3. iOS:获取图片Alpha图片

    -(void)createImages { // Load the alpha image, which is just the same Ship.png image used in the cli ...

  4. [iOS]把16进制(#871f78)颜色转换UIColor

    // // ViewController.m // text // // Created by 李东旭 on 16/1/22. // Copyright © 2016年 李东旭. All rights ...

  5. UIImagePickerController从拍照、图库、相册获取图片

    iOS 获取图片有三种方法: 1. 直接调用摄像头拍照 2. 从相册中选择 3. 从图库中选择 UIImagePickerController 是系统提供的用来获取图片和视频的接口: 用UIImage ...

  6. iOS 除去图片的白色背景(接近白色),或者其它颜色的替换,获取像素点的ARGB值

    iOS 除去图片的白色背景(接近白色),或者其它颜色的替换 方法如下: //去除图片的白色背景 + (UIImage*) imageToTransparent:(UIImage*) image { / ...

  7. ios获取摄像头与相册图片

    iOS的一些设备上都安装了摄像头.现在绝大多数都有了. 在编程中,我们是用相应的东西来进行照相,录像等功能.   一.UIImagePickerController类 UIImagePickerCon ...

  8. ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结

    相册 iphone的相册包含摄像头胶卷+用户计算机同步的部分照片.用户可以通过UIImagePickerController类提供的交互对话框来从相册中选择图像.但是,注意:相册中的图片机器路径无法直 ...

  9. ios 从网络上获取图片并在UIImageView中显示

    ios 从网络上获取图片   -(UIImage *) getImageFromURL:(NSString *)fileURL { NSLog(@"执行图片下载函数"); UIIm ...

随机推荐

  1. 【bzoj4260】Codechef REBXOR Trie树

    题目描述 输入 输入数据的第一行包含一个整数N,表示数组中的元素个数. 第二行包含N个整数A1,A2,…,AN. 输出 输出一行包含给定表达式可能的最大值. 样例输入 5 1 2 3 1 2 样例输出 ...

  2. 【Luogu】P2219修筑绿化带(单调队列)

    题目链接 这题各种边界判断恶心死人 就是单调队列在每行求出最小的.能装进A*B方块里的花坛 然后再在刚刚求出的那个东西里面跑一遍竖着的单调队列 然后……边界调了一小时 做完这题我深刻地感觉到我又强了 ...

  3. 【Luogu】P2962灯Lights(折半搜索)

    题目链接 本意是想学高斯消元,然后一顿乱搞之后学到了一个神奇的搜索方式叫做折半搜索. qwq 就是我先dfs前二分之n个点,然后再dfs后二分之n个点. 然后我dfs后二分之n个点的时候判断一下第一次 ...

  4. 刷题总结——pole(uva 1638 dp)

    题目: 题解: 这道题很妙的一点是很好地利用了最矮的杆子除了放两侧以外对观察数是没有影响的性质·· 考虑n-1个杆子与n个杆子··我们可以把n个杆子的排列看成n-1个杆子的长度加1按原来的排列顺序·· ...

  5. @login_required用法简介

    在django项目中,经常会看到下面这样的代码: from django.contrib.auth.decorators import login_required @login_required d ...

  6. 【NOIP2016练习】T3 subset (分块,状压DP)

    3 subset 3.1 题目  述 一开始你有一个空集,集合可以出现重复元素,然后有 Q 个操作 add s 在集合中加入数字 s. del s 在集合中删除数字 s.保证 s 存在 cnt s 查 ...

  7. poj 2492 A Bug's Life 二分图染色 || 种类并查集

    题目链接 题意 有一种\(bug\),所有的交往只在异性间发生.现给出所有的交往列表,问是否有可疑的\(bug\)(进行同性交往). 思路 法一:种类并查集 参考:https://www.2cto.c ...

  8. 04深入理解C指针之---指针优缺点

    该系列文章源于<深入理解C指针>的阅读与理解,由于本人的见识和知识的欠缺可能有误,还望大家批评指教. 指针的优点: 1.支持动态内存分配,主要借助于malloc()函数完成内存的分配,fr ...

  9. 共享内存之——system V共享内存

    System V 的IPC对象有共享内存.消息队列.信号灯(量). 注意:在IPC的通信模式下,不管是共享内存.消息队列还是信号灯,每个IPC的对象都有唯一的名字,称为"键(key)&quo ...

  10. Scrapy学习-15-降低被识别为爬虫的方法

    3种常见的方法 1. 在settings中配置禁用cookies COOKIES_ENABLED = False 2. scrapy限速处理,scrapy为我们提供了扩展模块,它能动态的限制下载速度 ...