UIImage_Category
来源:互联网 发布:java jvm原理 一句话 编辑:程序博客网 时间:2024/06/05 18:49
获取图片上一点的颜色
根据图片上的一点,获得对应的颜色值。
UIImage *image = [UIImage imageNamed:@"sns_icon_15"];UIColor *color = [self getColorAtPoint:CGPointMake(4, 44) atImage:image];
实现代码:
- (UIColor*) getColorAtPoint:(CGPoint)point atImage:(UIImage *)image { UIColor* color = nil; CGImageRef cgImage = image.CGImage; CGContextRef context = [self createARGBBitmapContextFromImage:cgImage]; if (context == NULL) { return nil; } size_t w = CGImageGetWidth(cgImage); size_t h = CGImageGetHeight(cgImage); CGRect rect = {{0,0},{w,h}}; CGContextDrawImage(context, rect, cgImage); unsigned char* data = CGBitmapContextGetData (context); if (data != NULL) { int offset = 4*((w*round(point.y))+round(point.x)); int alpha = data[offset]; int red = data[offset+1]; int green = data[offset+2]; int blue = data[offset+3]; color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue: (blue/255.0f) alpha:(alpha/255.0f)]; } CGContextRelease(context); if (data) { free(data); } return color;}- (CGContextRef)createARGBBitmapContextFromImage:(CGImageRef)inImage { CGContextRef context = NULL; CGColorSpaceRef colorSpace; void *bitmapData; int bitmapByteCount; int bitmapBytesPerRow; size_t pixelsWide = CGImageGetWidth(inImage); size_t pixelsHigh = CGImageGetHeight(inImage); // Declare the number of bytes per row. Each pixel in the bitmap in this // example is represented by 4 bytes; 8 bits each of red, green, blue, and // alpha. bitmapBytesPerRow = ((int)pixelsWide * 4); bitmapByteCount = (bitmapBytesPerRow * (int)pixelsHigh); // Use the generic RGB color space. colorSpace = CGColorSpaceCreateDeviceRGB(); if (colorSpace == NULL) { fprintf(stderr,"Error allocating color space\n"); return NULL; } // Allocate memory for image data. This is the destination in memory // where any drawing to the bitmap context will be rendered. bitmapData = malloc(bitmapByteCount); if (bitmapData == NULL) { fprintf(stderr,"Memory not allocated!"); CGColorSpaceRelease(colorSpace); return NULL; } // Create the bitmap context. We want pre-multiplied ARGB, 8-bits // per component. Regardless of what the source image format is // (CMYK, Grayscale, and so on) it will be converted over to the format // specified here by CGBitmapContextCreate. context = CGBitmapContextCreate(bitmapData, pixelsWide, pixelsHigh, 8, // bits per component bitmapBytesPerRow, colorSpace, kCGImageAlphaPremultipliedFirst); if (context == NULL) { free(bitmapData); fprintf(stderr,"Context not created!"); } // Make sure and release colorspace before returning CGColorSpaceRelease(colorSpace); return context;}
获得纯色image
(UIImage )imageWithColor:(UIColor )color size:(CGSize)size {
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();return image;
}
0 0
- UIImage_Category
- 软件性能测试的几种方法
- LibSVM(java版)的使用
- 阿里云服务器配置php+apache+mysql+phpMyadmin开发环境并上传本地代码
- 光流检测运动物体
- 【生活感触】:生活要稳健(只看标题即可)
- UIImage_Category
- 面试时必须准备的100 个关键问题!
- SOL学习笔记(3)之索引、唯一及主键约束
- iOS应用程序短信分享和邮件分享
- Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Not unique table/alias: 'e'
- python中%r和%s的区别
- Spring声明式事务管理(基于xml的方式)
- 对于scanf的新理解
- Android相机应用