2012-03-21 27 views
6

UIImageをJPEGとして保存するiOSでは、UIImageJPEGRepresentationを使用しますが、圧縮率以外のオプションは使用しません。私はUIImageprogressive JPEGフォーマットに保存したいのですが、簡単な方法はありますか?iOSでUIImageをプログレッシブJPEG形式に保存する方法は?

OS Xのように、NSImageをプログレッシブ形式に保存するためのNSImageProgressiveオプションがあります。

+0

あなたは 'UIImageJPEGRepresentation(<#UIImage *イメージ番号を>してみてくださいでした、 <#CGFloat compressionQuality#>) ' – hchouhan02

+0

これはプログレッシブにはなりません。 –

答えて

7

私はあなたがこのように、ImageIOでのフレームワークを使用してそれを行うことができると思います。

#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は、出力ファイルがプログレッシブであると言い、そしてImageMagickののidentifyコマンドは、それが「インターレース:JPEG」を持っていると言います。

+0

私はiOSデバイスでテストをしており、異なるフォームシミュレータです。 – user501836

+0

結果はhttp://stackoverflow.com/questions/10829100/creating-progressive-jpeg-on-ios-with-imageio-produces-blocky-results-on-deviceと同じです。 – user501836

0

このコードは、デバイス上の仕事がない - 鍵は(その使用して新しいリテラル構文に注意してください)すべての密度値を指定するには、次のとおりです。

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

'(__bridge id) kCFBooleanTrue、kCGImagePropertyJFIFIsProgressive'は 'jfifProperties'の一部ですか?私はそれを実際にプログレッシブ画像として保存するために追加する必要がありました。 – qix

+0

@ Linus、私はこの質問に答えたときにこれを逃したか、そうした財産はなかった。明らかに、この回答はオリジナルのポスターではない人にとってはまったく役に立たなかったので、疑念を持ってここにあるものを見ることになるでしょう(もちろん、私は正しいか、それとも投稿しませんでした:-)) –

関連する問題