#import "ViewController.h"
typedef enum {
ALPHA = 0,
BLUE = 1,
GREEN = 2,
RED = 3
} PIXELS;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *srcImage = [UIImage imageNamed:@"1.png"];
UIImage *grayImage1 = [self convertToGrayscale:srcImage];
UIImage *grayImage2 = [self createGrayCopy:srcImage];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 320, 150)];
imageView.image = srcImage;
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 175, 320, 150)];
imageView1.image = grayImage1;
imageView1.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView1];
UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 330, 320, 150)];
imageView2.image = grayImage2;
imageView2.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView2];
}
// 方法一:CGColorSpaceCreateDeviceRGB创建一个颜色空间引用
- (UIImage *)convertToGrayscale:(UIImage *)source {
CGSize size = [source size];
int width = size.width;
int height = size.height;
// the pixels will be painted to this array
uint32_t *pixels = (uint32_t *) malloc(width * height * sizeof(uint32_t));
// clear the pixels so any transparency is preserved
memset(pixels, 0, width * height * sizeof(uint32_t));
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
// create a context with RGBA pixels
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);
// paint the bitmap to our context which will fill in the pixels array
CGContextDrawImage(context, CGRectMake(0, 0, width, height), source.CGImage);
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
uint8_t *rgbaPixel = (uint8_t *) &pixels[y * width + x];
// convert to grayscale using recommended method: http://en.wikipedia.org/wiki/Grayscale#Converting_color_to_grayscale
uint32_t gray = 0.3 * rgbaPixel[RED] + 0.59 * rgbaPixel[GREEN] + 0.11 * rgbaPixel[BLUE];
// set the pixels to gray
rgbaPixel[RED] = gray;
rgbaPixel[GREEN] = gray;
rgbaPixel[BLUE] = gray;
}
}
// create a new CGImageRef from our context with the modified pixels
CGImageRef image = CGBitmapContextCreateImage(context);
// we're done with the context, color space, and pixels
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
free(pixels);
// make a new UIImage to return
UIImage *resultUIImage = [UIImage imageWithCGImage:image];
// we're done with image now too
CGImageRelease(image);
return resultUIImage;
}
//方法二: CGColorSpaceCreateDeviceGray会创建一个设备相关的灰度颜色空间的引用
- (UIImage *)createGrayCopy:(UIImage *)source{
int width = source.size.width;
int height = source.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef context = CGBitmapContextCreate (nil,
width,
height,
8, // bits per component
0,
colorSpace,
kCGBitmapByteOrderDefault);
CGColorSpaceRelease(colorSpace);
if (context == NULL) {
return nil;
}
CGContextDrawImage(context,
CGRectMake(0, 0, width, height), source.CGImage);
UIImage *grayImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)];
CGContextRelease(context);
return grayImage;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
浙公网安备 33010602011771号