UIImage_Category
2015-11-29 21:32
393 查看
获取图片上一点的颜色
根据图片上的一点,获得对应的颜色值。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;
}
相关文章推荐
- 按右键另存图片只能存BMP
- photoshop去除图片上的水印
- upload上传单张图片
- 图片引发的溢出危机(图)
- C#实现把彩色图片灰度化代码分享
- C#将图片和字节流互相转换并显示到页面上
- C#监控文件夹并自动给图片文件打水印的方法
- 纯CSS实现的当鼠标移上图片添加阴影效果代码
- C#实现打开画图的同时载入图片、最大化显示画图窗体的方法
- 随鼠标移动的图片或文字特效代码
- CSS 图片横向排列实现代码
- C#实现将Email地址转成图片显示的方法
- 超级经典一套鼠标控制左右滚动图片带自动翻滚
- 用css实现图片垂直居中的使用技巧
- C++实现读取图片长度和宽度
- PHP根据图片色界在不同位置加水印的方法
- php通过修改header强制图片下载的方法
- PHP安全上传图片的方法
- 使用GD库生成带阴影文字的图片
- php获取网页里所有图片并存入数组的方法