- (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. div固定在屏幕底部

    项目中需要实现div一直显示在屏幕的底部,不管页面有多长或者多端都需要满足. 在网上找的用js实现的,移动时会闪动,效果不是特别好.也可能是没找到好的. 相比js,我更希望使用css实现 1 < ...

  2. iOS学习笔记31-从图册获取图片和视频

    一.从图册中获取本地图片和视频 从图册中获取文件,我们使用的是UIImagePickerController,这个类我们在之前的摄像头中使用过,这里是链接:iOS学习笔记27-摄像头,这里我们使用的是 ...

  3. 【Luogu】P2801教主的魔法(分块)

    题目链接 激动qwq.这是我A的第一道分块. 分块之后对块内元素暴力sort.修改的时候对于整块打个标记,查询的时候只需要查C-tag就行了 对于非整块,暴力修改,改完之后sort 对于查询……非整块 ...

  4. 【Luogu】P3159交换棋子(超出我能力范围的费用流)

    题目链接 明显超出我能力范围. 只放题解. 再放代码. #include<cstring> #include<algorithm> #include<cstdio> ...

  5. Resource 定位、BeanDefinition 的载入和解析,BeanDefinition 注册。

    在前文提过,IOC 容器的初始化过程分为三步骤:Resource 定位.BeanDefinition 的载入和解析,BeanDefinition 注册. Resource 定位.我们一般用外部资源来描 ...

  6. 常用jar包下载地址汇总

    <常用开发包下载地址汇总> 下载Jackson http://www.java2s.com/Code/Jar/j/Downloadjacksonjar.htm 下载Simple Loggi ...

  7. [USACO12MAR]花盆Flowerpot (单调队列,二分答案)

    题目链接 Solution 转化一下,就是个单调队列. 可以发现就是一段区间 \([L,R]\) 使得其高度的极差不小于 \(d\) ,同时满足 \(R-L\) 最小. 然后可以考虑二分然后再 \(O ...

  8. iOS-BMK标注&覆盖物

    在iOS开发中,地图算是一个比较重要的模块.我们常用的地图有高德地图,百度地图,谷歌地图,对于中国而言,苹果公司已经不再使用谷歌地图,官方使用的是高德地图.下面将讲述一下百度地图开发过程中的一些小的知 ...

  9. linux之Deamon进程创建及其进程exit,_exit,return之间的区别

    Dameon进程又被称做守护进程,一般来说他有以下2个特点: 1.生命周期非常长,一旦启动,一般不会终止,直到系统推出,不过dameon进程可以通过stop或者发送信号将其杀死 2.在后台执行,不跟任 ...

  10. AC日记——爱改名的小融3 codevs 3156

    3156 爱改名的小融 3  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description Wikioi上有个人叫小融,他喜 ...