2012-03-21 10 views
6

En iOS para guardar un UIImage como JPEG, uso UIImageJPEGRepresentation, sin embargo, no toma otras opciones que la relación de compresión. Deseo guardar el UIImage en el formato progressive JPEG, ¿hay alguna manera fácil de hacerlo?Cómo guardar un UIImage en formato JPEG progresivo en iOS?

Parece que en OS X hay una opción NSImageProgressive para guardar NSImage en formato progresivo.

+0

¿Usted intentó UIImageJPEGRepresentation (<# UIImage * Nº de imagen> ', <#CGFloat compressionQuality #>) ' – hchouhan02

+0

Eso no lo hace progresivo. –

Respuesta

7

creo que puede hacerlo utilizando el marco ImageIO, así:

#import <UIKit/UIKit.h> 
#import <ImageIO/ImageIO.h> 
#import <MobileCoreServices/MobileCoreServices.h> 
#import "AppDelegate.h" 

int main(int argc, char *argv[]) 
{ 
    @autoreleasepool { 
     UIImage *sourceImage = [UIImage imageNamed:@"test.jpg"]; 

     CFURLRef url = CFURLCreateWithString(NULL, CFSTR("file:///tmp/progressive.jpg"), NULL); 
     CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypeJPEG, 1, NULL); 
     CFRelease(url); 

     NSDictionary *jfifProperties = [NSDictionary dictionaryWithObjectsAndKeys: 
      (__bridge id)kCFBooleanTrue, kCGImagePropertyJFIFIsProgressive, 
      nil]; 

     NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys: 
      [NSNumber numberWithFloat:.6], kCGImageDestinationLossyCompressionQuality, 
      jfifProperties, kCGImagePropertyJFIFDictionary, 
      nil]; 

     CGImageDestinationAddImage(destination, sourceImage.CGImage, (__bridge CFDictionaryRef)properties); 
     CGImageDestinationFinalize(destination); 
     CFRelease(destination); 

     return 0; 
    } 
} 

Preview.app dice que el archivo de salida es progresiva, y el comando de ImageMagick identify dice que tiene “entrelazado: JPEG”.

+0

Tengo una prueba en el dispositivo iOS, y soy simulador de formulario diferente. – user501836

+0

El resultado es el mismo con http://stackoverflow.com/questions/10829100/creating-progressive-jpeg-on-ios-with-imageio-produces-blocky-results-on-device – user501836

0

Este código funciona en el dispositivo - la clave es especificar todos los valores de densidad (tenga en cuenta su uso de nueva sintaxis literal):

- (UIImage *)imager 
{ 
    UIImage *object = [UIImage imageNamed:@"Untitled.jpg"]; 
    NSString *str = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"Tester.jpg"]; 
    NSURL *url = [[NSURL alloc] initFileURLWithPath:str]; 

    CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)url, kUTTypeJPEG, 1, NULL); 

    NSDictionary *jfifProperties = [NSDictionary dictionaryWithObjectsAndKeys: 
            @72, kCGImagePropertyJFIFXDensity, 
            @72, kCGImagePropertyJFIFYDensity, 
            @1, kCGImagePropertyJFIFDensityUnit, 
            nil]; 

    NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys: 
           [NSNumber numberWithFloat:.5f], kCGImageDestinationLossyCompressionQuality, 
           jfifProperties, kCGImagePropertyJFIFDictionary, 
           nil]; 

    CGImageDestinationAddImage(destination, ((UIImage*)object).CGImage, (__bridge CFDictionaryRef)properties); 
    CGImageDestinationFinalize(destination); 
    CFRelease(destination); 

    return [UIImage imageWithContentsOfFile:str]; 
} 
+0

No debería '(__bridge id) kCFBooleanTrue, kCGImagePropertyJFIFIsProgressive' ser parte de 'jfifProperties'? Tuve que agregar eso para realmente guardarlo como una imagen progresiva. – qix

+0

@Linus, cuando respondí esta pregunta o me perdí esto, o no había tal propiedad. Evidentemente, esta respuesta no ha sido de utilidad para nadie, desde luego, no para el póster original, así que vería lo que está aquí con sospecha (aunque, por supuesto, estoy bastante seguro de que es correcto o no lo habría publicado :-)) –

Cuestiones relacionadas