Making a UIImageView grey

后端 未结 4 916
离开以前
离开以前 2021-01-12 03:35

I\'m setting UImageViews within table cells using setImageWithUrl from the AFNetworking library, but I need the images to be greyscale... is there any way I can do this. I\'

4条回答
  •  抹茶落季
    2021-01-12 04:24

    Try this method:

    - (UIImage *)convertImageToGrayScale:(UIImage *)image
    {
      // Create image rectangle with current image width/height
      CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
    
      // Grayscale color space
      CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
    
      // Create bitmap content with current image size and grayscale colorspace
      CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone);
    
      // Draw image into current context, with specified rectangle
      // using previously defined context (with grayscale colorspace)
      CGContextDrawImage(context, imageRect, [image CGImage]);
    
      // Create bitmap image info from pixel data in current context
      CGImageRef imageRef = CGBitmapContextCreateImage(context);
    
      // Create a new UIImage object  
      UIImage *newImage = [UIImage imageWithCGImage:imageRef];
    
      // Release colorspace, context and bitmap information
      CGColorSpaceRelease(colorSpace);
      CGContextRelease(context);
      CFRelease(imageRef);
    
      // Return the new grayscale image
      return newImage;
    }
    

    I found it here: http://mobiledevelopertips.com/graphics/convert-an-image-uiimage-to-grayscale.html

提交回复
热议问题