Objective-C Tips & Trick - Prioregroup.com
[Objective-C] Save jpeg image from CGImageRef
       
back

#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/UTCoreTypes.h>

static void CGImageWriteToFile(CGImageRef image, NSString *path, float compression)
{
    NSURL *url = [NSURL URLWithString:path];
    if (![[url scheme] isEqualToString:@"file"])
        url = [NSURL fileURLWithPath:path];
    
    NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
                                @(compression), kCGImageDestinationLossyCompressionQuality,
                                nil];
    CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)url, kUTTypeJPEG, 1, (CFDictionaryRef)properties);
    CGImageDestinationAddImage(destination, image, nil);
    
    if (!CGImageDestinationFinalize(destination)) {
        NSLog(@"Failed to write image to %@", path);
    }
    
    CFRelease(destination);
}






Copyright © 1996-2018 Centro Studi Informatica di Danilo Priore. All rights reserved. P.I.10149810581.
prioregroup.com