iOS 获取图片某一点的颜色对象(UIColor*)。
- (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*)。的更多相关文章
- iOS获取图片的Base64String,兼容Android,java,web,jpg(jpeg),png
呃呃呃……需求的来源又是同学,对!又是! 废话不哆嗦,怎么把一张图在iOS上转一个Base64String出来,稍微了解的,或者随便搜一下,都能搞定一大堆,但是!!!! 自己(iOS)转自己用,完全没 ...
- iOS获取图片格式
我们系统使用的一般文件格式有png.jpg.jpeg.GIF.TIFF 等格式: 图片是我们开发过程中最常见的东西,一般每个公司都会有自己的文件服务器,客户端人员都会从文件服务器中获取相关的图片信息: ...
- iOS:获取图片Alpha图片
-(void)createImages { // Load the alpha image, which is just the same Ship.png image used in the cli ...
- [iOS]把16进制(#871f78)颜色转换UIColor
// // ViewController.m // text // // Created by 李东旭 on 16/1/22. // Copyright © 2016年 李东旭. All rights ...
- UIImagePickerController从拍照、图库、相册获取图片
iOS 获取图片有三种方法: 1. 直接调用摄像头拍照 2. 从相册中选择 3. 从图库中选择 UIImagePickerController 是系统提供的用来获取图片和视频的接口: 用UIImage ...
- iOS 除去图片的白色背景(接近白色),或者其它颜色的替换,获取像素点的ARGB值
iOS 除去图片的白色背景(接近白色),或者其它颜色的替换 方法如下: //去除图片的白色背景 + (UIImage*) imageToTransparent:(UIImage*) image { / ...
- ios获取摄像头与相册图片
iOS的一些设备上都安装了摄像头.现在绝大多数都有了. 在编程中,我们是用相应的东西来进行照相,录像等功能. 一.UIImagePickerController类 UIImagePickerCon ...
- ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结
相册 iphone的相册包含摄像头胶卷+用户计算机同步的部分照片.用户可以通过UIImagePickerController类提供的交互对话框来从相册中选择图像.但是,注意:相册中的图片机器路径无法直 ...
- ios 从网络上获取图片并在UIImageView中显示
ios 从网络上获取图片 -(UIImage *) getImageFromURL:(NSString *)fileURL { NSLog(@"执行图片下载函数"); UIIm ...
随机推荐
- manacher-模板-hd-3068
/* 题意:给一个字符串,求该串的最长回文串的长度: 算法:Manacher O(n)复杂度,求以每一个字符为中心的最长汇文串的长度: 这个算法把奇数和偶数的情况和在一起来考虑了: */ #inclu ...
- jq 的replaceWith方法在360下面会出现兼容问题
弄的繁琐点, 先remove旧的元素,然后append就好了
- hdu 4353 统计点在三角形内的个数
Finding Mine Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- 欧拉函数之和(51nod 1239)
对正整数n,欧拉函数是小于或等于n的数中与n互质的数的数目.此函数以其首名研究者欧拉命名,它又称为Euler's totient function.φ函数.欧拉商数等.例如:φ(8) = 4(Phi( ...
- UVa11542 Square
/*by SilverN*/ #include<iostream> #include<algorithm> #include<cstring> #include&l ...
- 【NOIP2015】信息传递(强连通分量)
题意:找一张图中的最小环 O(n) 思路:强连通分量tarjan即可 注意环中节点数>1 ..]of longint; n,i,ans,tot,id,top,time,x:longint; pr ...
- WPF,布局,Menu,MenuItem,DockPanel,Grid,DockPanel.Dock='',ToolBar,Content,Image,Uri
布局相关: Grid as title Binding Path="", Mode= TwoWay, model should implement IPropertyChanged ...
- gpio_direction_output vs gpio_set_value之间的使用关系
在Linux驱动中常常会碰到gpio_set_value(port_num,0/1)或gpio_direction_output (port_num,0/1) 这两者有什么关系呢gpio_set_va ...
- UICollectionView的cell创建直接从第三个数据开始问题
实现的效果是这样 大概意思就是第一组没有数据就直接将改组的cell高度变成0效果实现了,但是第二组数据创建cell就出问题了--奇葩问题 * 代码问题在这```-(CGSize)collectionV ...
- CSS-点赞爱心小动画
爱心图片: HTML: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...