转自:http://blog.sina.com.cn/s/blog_7ccde1bf0100v9oj.html
1 - (UIImage *) convertToGreyscale:(UIImage *)i {
2 int kRed = 1;
3 int kGreen = 2;
4 int kBlue = 4;
5 int colors = kGreen;
6 int m_width = i.size.width;
7 int m_height = i.size.height;
8 uint32_t *rgbImage = (uint32_t *) malloc(m_width * m_height * sizeof(uint32_t));
9 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
10 CGContextRef context = CGBitmapContextCreate(rgbImage, m_width, m_height, 8, m_width * 4, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
11 CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
12 CGContextSetShouldAntialias(context, NO);
13 CGContextDrawImage(context, CGRectMake( 0, 0, m_width, m_height), [i CGImage]);
14 CGContextRelease(context);
15 CGColorSpaceRelease(colorSpace);
16 // now convert to grayscale
17 uint8_t *m_imageData = (uint8_t *) malloc(m_width * m_height);
18 for( int y = 0; y < m_height; y++) {
19 for( int x = 0; x < m_width; x++) {
20 uint32_t rgbPixel=rgbImage[y*m_width+x];
21 uint32_t sum= 0,count= 0;
22 if (colors & kRed) {sum += (rgbPixel>> 24)& 255; count++;}
23 if (colors & kGreen) {sum += (rgbPixel>> 16)& 255; count++;}
24 if (colors & kBlue) {sum += (rgbPixel>> 8)& 255; count++;}
25 m_imageData[y*m_width+x]=sum/count;
26 }
27 }
28 free(rgbImage);
29 // convert from a gray scale image back into a UIImage
30 uint8_t *result = (uint8_t *) calloc(m_width * m_height * sizeof(uint32_t), 1);
31 // process the image back to rgb
32 for( int i = 0; i < m_height * m_width; i++) {
33 result[i* 4]= 0;
34 int val=m_imageData[i];
35 result[i* 4+ 1]=val;
36 result[i* 4+ 2]=val;
37 result[i* 4+ 3]=val;
38 }
39 // create a UIImage
40 colorSpace = CGColorSpaceCreateDeviceRGB();
41 context = CGBitmapContextCreate(result, m_width, m_height, 8, m_width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
42 CGImageRef image = CGBitmapContextCreateImage(context);
43 CGContextRelease(context);
44 CGColorSpaceRelease(colorSpace);
45 UIImage *resultUIImage = [UIImage imageWithCGImage:image];
46 CGImageRelease(image);
47 // make sure the data will be released by giving it to an autoreleased NSData
48 [NSData dataWithBytesNoCopy:result length:m_width * m_height];
49 return resultUIImage;
50 }
2 int kRed = 1;
3 int kGreen = 2;
4 int kBlue = 4;
5 int colors = kGreen;
6 int m_width = i.size.width;
7 int m_height = i.size.height;
8 uint32_t *rgbImage = (uint32_t *) malloc(m_width * m_height * sizeof(uint32_t));
9 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
10 CGContextRef context = CGBitmapContextCreate(rgbImage, m_width, m_height, 8, m_width * 4, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
11 CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
12 CGContextSetShouldAntialias(context, NO);
13 CGContextDrawImage(context, CGRectMake( 0, 0, m_width, m_height), [i CGImage]);
14 CGContextRelease(context);
15 CGColorSpaceRelease(colorSpace);
16 // now convert to grayscale
17 uint8_t *m_imageData = (uint8_t *) malloc(m_width * m_height);
18 for( int y = 0; y < m_height; y++) {
19 for( int x = 0; x < m_width; x++) {
20 uint32_t rgbPixel=rgbImage[y*m_width+x];
21 uint32_t sum= 0,count= 0;
22 if (colors & kRed) {sum += (rgbPixel>> 24)& 255; count++;}
23 if (colors & kGreen) {sum += (rgbPixel>> 16)& 255; count++;}
24 if (colors & kBlue) {sum += (rgbPixel>> 8)& 255; count++;}
25 m_imageData[y*m_width+x]=sum/count;
26 }
27 }
28 free(rgbImage);
29 // convert from a gray scale image back into a UIImage
30 uint8_t *result = (uint8_t *) calloc(m_width * m_height * sizeof(uint32_t), 1);
31 // process the image back to rgb
32 for( int i = 0; i < m_height * m_width; i++) {
33 result[i* 4]= 0;
34 int val=m_imageData[i];
35 result[i* 4+ 1]=val;
36 result[i* 4+ 2]=val;
37 result[i* 4+ 3]=val;
38 }
39 // create a UIImage
40 colorSpace = CGColorSpaceCreateDeviceRGB();
41 context = CGBitmapContextCreate(result, m_width, m_height, 8, m_width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
42 CGImageRef image = CGBitmapContextCreateImage(context);
43 CGContextRelease(context);
44 CGColorSpaceRelease(colorSpace);
45 UIImage *resultUIImage = [UIImage imageWithCGImage:image];
46 CGImageRelease(image);
47 // make sure the data will be released by giving it to an autoreleased NSData
48 [NSData dataWithBytesNoCopy:result length:m_width * m_height];
49 return resultUIImage;
50 }