priore

Save jpeg image from CGImageRef

Apr 23rd, 2015
869
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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×