- (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. BZOJ1879 [Sdoi2009]Bill的挑战 【状压dp】

    题目 输入格式 本题包含多组数据. 第一行:一个整数T,表示数据的个数. 对于每组数据: 第一行:两个整数,N和K(含义如题目表述). 接下来N行:每行一个字符串. T ≤ 5,M ≤ 15,字符串长 ...

  2. 如何查找Windows上安装的DB2的端口号Port

    1.db2com打开控制台 2.db2 get dbm cfg 可以通过db2 get dbm cfg,查询数据库管理器配置参数,就可以查到端口号或端口名. 示例: $ db2 get dbm cfg ...

  3. hdu 4418 高斯消元求期望

    Time travel Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. 【CF1028B】Unnatural Conditions(构造)

    题意:给定n与m,要求构造两个长度不超过2230的数字x,y使得x,y各自的数位和>=n,x+y的数位和<=n 思路: #include<cstdio> #include< ...

  5. 【CF1016B】Segment Occurrences(模拟)

    题意:给定两个串s和t,多次询问s的一个区间[l ,r]中有多少个子串与t串相同 len<=1e3,q<=1e5 思路:前缀和 #include<cstdio> #includ ...

  6. jquery 日期插件

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. Linux 之 文件压缩解压

    文件压缩解压 参考教程:[千峰教育] 命令: gzip: 作用:压缩文件,只能是单个文件,不能是多个,也不能是目录. 格式:gzip file 说明:执行命令会生成file.gz,删除原来的file ...

  8. SPOJ GSS系列(数据结构维护技巧入门)

    题目链接 GSS $GSS1$ 对于每个询问$l$, $r$,查询$a_{l}$, $a_{l+1}$, $a_{l+2}$, ..., $a_{r}$这个序列的最大字段和. 建立线段树,每个节点维护 ...

  9. redis基本类型和操作

    基本类型:string hash list set sorted set 添加String 类型(最基本的key,value形式) set str1 s1 获取value get str1 添加has ...

  10. 新华三孟丹:NFV资源池实现中的技术探讨

    近日,在第三届未来网络发展大会SDN/NFV技术与应用创新分论坛上,新华三解决方案部架构师孟丹女士发表了主题为<NFV资源池实现中的技术探讨>的主题演讲. 孟丹指出,新华三的NFV核心理念 ...