cocoaTouch/otherSrc/UIImageExtra.m
changeset 3463 23c50be687a9
parent 3361 cfc6cd502f85
child 3492 07256e1ad559
equal deleted inserted replaced
3462:4b36933dce1d 3463:23c50be687a9
    75     } else {
    75     } else {
    76         NSLog(@"initWithContentsOfFile: andCutAt: FAILED");
    76         NSLog(@"initWithContentsOfFile: andCutAt: FAILED");
    77         return nil;
    77         return nil;
    78     }
    78     }
    79 }
    79 }
       
    80 
       
    81 -(UIImage *)convertImageToGrayScale:(UIImage *)image {
       
    82   // Create image rectangle with current image width/height
       
    83   CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
       
    84  
       
    85   // Grayscale color space
       
    86   CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
       
    87  
       
    88   // Create bitmap content with current image size and grayscale colorspace
       
    89   CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone);
       
    90  
       
    91   // Draw image into current context, with specified rectangle
       
    92   // using previously defined context (with grayscale colorspace)
       
    93   CGContextDrawImage(context, imageRect, [image CGImage]);
       
    94  
       
    95   // Create bitmap image info from pixel data in current context
       
    96   CGImageRef imageRef = CGBitmapContextCreateImage(context);
       
    97  
       
    98   // Create a new UIImage object  
       
    99   UIImage *newImage = [UIImage imageWithCGImage:imageRef];
       
   100  
       
   101   // Release colorspace, context and bitmap information
       
   102   CGColorSpaceRelease(colorSpace);
       
   103   CGContextRelease(context);
       
   104   CFRelease(imageRef);
       
   105  
       
   106   // Return the new grayscale image
       
   107   return newImage;
       
   108 }
    80  
   109  
    81 @end
   110 @end