2012-03-21 8 views
6

In iOS per salvare un UIImage come JPEG, io uso UIImageJPEGRepresentation, tuttavia non accetta opzioni diverse dal rapporto di compressione. Desidero salvare lo UIImage nel formato progressive JPEG, c'è un modo semplice per farlo?Come salvare un UIImage in formato JPEG progressivo in iOS?

Sembra in OS X c'è un'opzione NSImageProgressive per salvare NSImage in formato progressivo.

+0

Hai provato 'UIImageJPEGRepresentation (<# UIImage * immagine #>, <#CGFloat compressionQuality #>) ' – hchouhan02

+0

Ciò non lo rende progressivo. –

risposta

7

penso che si può farlo utilizzando il framework ImageIO, in questo modo:

#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 che il file di output è progressiva, e il comando di ImageMagick identify dice che ha “Interlace: JPEG”.

+0

Ho un test sul dispositivo iOS e sono un simulatore di forme diverse. – user501836

+0

Il risultato sono gli stessi con http://stackoverflow.com/questions/10829100/creating-progressive-jpeg-on-ios-with-imageio-produces-blocky-results-on-device – user501836

0

Questo codice funziona sul dispositivo - la chiave è quello di specificare tutti i valori di densità (nota la sua utilizzando nuova sintassi letterale):

- (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

Non dovrebbe '(__bridge id) kCFBooleanTrue, kCGImagePropertyJFIFIsProgressive 'fa parte del' jfifProperties'? Ho dovuto aggiungerlo per farlo davvero salvare come immagine progressiva. – qix

+0

@Linus, quando ho risposto a questa domanda o ho perso questo, o non c'era tale proprietà. Evidentemente questa risposta è stato di alcuna utilità per chiunque di certo non il manifesto originale, quindi vorrei vedere ciò che è qui con sospetto (anche se, naturalmente, sono abbastanza sicuro che il suo corretto o non avrei postato :-)) –