Κυριακή, 12 Μαΐου 2013

Transform UIImage into Black'n'White image



source here:


- (UIImage *)getBlackAndWhiteVersionOfImage:(UIImage *)anImage {
 UIImage *newImage;
 
 if (anImage) {
  CGColorSpaceRef colorSapce = CGColorSpaceCreateDeviceGray();
  CGContextRef context = CGBitmapContextCreate(nil, anImage.size.width * anImage.scale, anImage.size.height * anImage.scale, 8, anImage.size.width * anImage.scale, colorSapce, kCGImageAlphaNone);
  CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
  CGContextSetShouldAntialias(context, NO);
  CGContextDrawImage(context, CGRectMake(0, 0, anImage.size.width, anImage.size.height), [anImage CGImage]);
  
  CGImageRef bwImage = CGBitmapContextCreateImage(context);
  CGContextRelease(context);
  CGColorSpaceRelease(colorSapce);
  
  UIImage *resultImage = [UIImage imageWithCGImage:bwImage];
  CGImageRelease(bwImage);
  
  UIGraphicsBeginImageContextWithOptions(anImage.size, NO, anImage.scale);
  [resultImage drawInRect:CGRectMake(0.0, 0.0, anImage.size.width, anImage.size.height)];
  newImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();
 }
 
 return newImage;
}

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου