How can I change the saturation of an UIImage?

后端 未结 6 1550
旧巷少年郎
旧巷少年郎 2021-01-30 05:42

I have an UIImage and want to shift it\'s saturation about +10%. Are there standard methods or functions that can be used for this?

6条回答
  •  北荒
    北荒 (楼主)
    2021-01-30 06:21

    Here is an implementation of Bessey's hack (put this code in a UIImage category). It ain't fast and it definitely shifts hues, but it sort of works.

    + (CGFloat) clamp:(CGFloat)pixel
    {
          if(pixel > 255) return 255;
          else if(pixel < 0) return 0;
          return pixel;
    }
    
    - (UIImage*) saturation:(CGFloat)s
    {
          CGImageRef inImage = self.CGImage;
          CFDataRef ref = CGDataProviderCopyData(CGImageGetDataProvider(inImage)); 
          UInt8 * buf = (UInt8 *) CFDataGetBytePtr(ref); 
          int length = CFDataGetLength(ref);
    
          for(int i=0; i

    Anyone have any ideas on how to improve this without doing a full HSV conversion? Or better yet, a true implementation for:

    - (UIImage*) imageWithHueOffset:(CGFloat)h saturation:(CGFloat)s value:(CGFloat)v
    

提交回复
热议问题