priore

Save jpeg image from CGImageRef

Apr 23rd, 2015
771
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import <ImageIO/ImageIO.h>
  2. #import <MobileCoreServices/UTCoreTypes.h>
  3.  
  4. static void CGImageWriteToFile(CGImageRef image, NSString *path, float compression)
  5. {
  6.     NSURL *url = [NSURL URLWithString:path];
  7.     if (![[url scheme] isEqualToString:@"file"])
  8.         url = [NSURL fileURLWithPath:path];
  9.    
  10.     NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
  11.                                 @(compression), kCGImageDestinationLossyCompressionQuality,
  12.                                 nil];
  13.     CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)url, kUTTypeJPEG, 1, (CFDictionaryRef)properties);
  14.     CGImageDestinationAddImage(destination, image, nil);
  15.    
  16.     if (!CGImageDestinationFinalize(destination)) {
  17.         NSLog(@"Failed to write image to %@", path);
  18.     }
  19.    
  20.     CFRelease(destination);
  21. }
RAW Paste Data