title: how to implement night mode book reader in iOS
body: <p>I tried to invert colors for an image to simulate <strong>night mode</strong> in a pdf reader I am working on, so I tried to use one's complement. I have an issue that the image gets moved to the right also there is some noise.</p>
  
  <p>So I am asking if there a better way, Please show me. Also if there is a pdf reader library with night mode implemented, You can tell me about it and save the day for me.</p>
  
  <p><strong>Note:</strong> </p>
  
  <p>1- I used code from this link: <a href="http://five.agency/iphone-image-processing/" rel="nofollow noreferrer">http://five.agency/iphone-image-processing/</a></p>
  
  <p>2- The book is pdf and the pages are as images not text.</p>
  
  <p>3- I am attaching the test image and the result.</p>
  
  <pre><code>self.imageView.image = [self onesComplement:image];
  </code></pre>
  
  <p>....</p>
  
  <pre><code>-(UIImage *)onesComplement:(UIImage *) image
  {
  
  CGImageRef sourceImage = image.CGImage;
  CFDataRef theData;
  theData = CGDataProviderCopyData(CGImageGetDataProvider(sourceImage));
  UInt8 *pixelData = (UInt8 *) CFDataGetBytePtr(theData);
  long dataLength = CFDataGetLength(theData);
  int red = 0;
  int green = 1;
  int blue = 2;
  for (int index = 0; index &lt; dataLength; index +=4) {
      pixelData[index + green] = ~ pixelData[index + green];
          pixelData[index + red] = ~ pixelData[index + red];
          pixelData[index + blue] = ~ pixelData[index + blue];
  
  }
  short bitsPerComponent = 8;
  CGContextRef context;
  context = CGBitmapContextCreate(pixelData,
                                  CGImageGetWidth(sourceImage),
                                  CGImageGetHeight(sourceImage),
                                  bitsPerComponent,
                                  CGImageGetBytesPerRow(sourceImage),
                                  CGImageGetColorSpace(sourceImage),
                                  kCGBitmapByteOrderDefault);
  
  CGImageRef newCGImage = CGBitmapContextCreateImage(context);
  UIImage *newImage = [UIImage imageWithCGImage:newCGImage];
  CGContextRelease(context);
  CFRelease(theData);
  CGImageRelease(newCGImage);
  return  newImage;
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/K6OI6.png" alt="Test image"></p>
  
  <p><img src="https://i.stack.imgur.com/DZ2QE.png" alt="Result image"></p>