iphone 將圖片處理成灰色(Gray)的

modun1986發表於2011-05-28

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,
						 kCGImageAlphaNone);

	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;
}
 

相關文章