2012-02-10 3 views
2

私は、ユーザーがJPEG画像を書き出すことを可能にするCocoa Mac画像編集アプリケーションを持っています。私は現在、JPEGファイルとしてこれらの画像をエクスポートするには、次のコードを使用しています。ユーザーはこのJPEG画像の1インチあたりのピクセルを提供できるようにするために、私は希望、しかしCocoaアプリケーションでエクスポートされたJPEG画像の1インチあたりのピクセル数を設定するにはどうすればよいですか?

//this is user specified 
NSInteger resolution; 

NSImage* savedImage = [[NSImage alloc] initWithSize:NSMakeSize(600, 600)]; 
[savedImage lockFocus]; 
//draw here 
[savedImage unlockFocus]; 

NSBitmapImageRep* savedImageBitmapRep = [NSBitmapImageRep imageRepWithData:[savedImage TIFFRepresentationUsingCompression:NSTIFFCompressionNone factor:1.0]]; 

NSDictionary* properties = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:1.0], NSImageCompressionFactor, nil]; 

//holds the jpeg file 
NSData * imageData = nil; 
imageData = [savedImageBitmapRep representationUsingType:NSJPEGFileType properties:properties]; 

(あなたがPhotoshopの中ですることができますようにエクスポートオプション)。エクスポートされたJPEGのこの値を調整するには、上記のコードを変更する必要がありますか?

答えて

2

NSImage APIで行う方法が見つかりませんでしたが、CGImageはkCGImagePropertyDPIHeight/Widthを設定することで可能でした。

私は、NSImageCompressionFactorと同じだと思うkCGImageDestinationLossyCompressionQualityも設定します。

//this is user specified 
NSInteger resolution = 100; 

NSImage* savedImage = [[NSImage alloc] initWithSize:NSMakeSize(600, 600)]; 
[savedImage lockFocus]; 
//draw here 
[savedImage unlockFocus]; 

NSBitmapImageRep* savedImageBitmapRep = [NSBitmapImageRep imageRepWithData:[savedImage TIFFRepresentationUsingCompression:NSTIFFCompressionNone factor:1.0]]; 

NSDictionary* properties = [NSDictionary dictionaryWithObjectsAndKeys: 
          [NSNumber numberWithFloat:1.0], kCGImageDestinationLossyCompressionQuality, 
          [NSNumber numberWithInteger:resolution], kCGImagePropertyDPIHeight, 
          [NSNumber numberWithInteger:resolution], kCGImagePropertyDPIWidth, 
          nil]; 

NSMutableData* imageData = [NSMutableData data]; 
CGImageDestinationRef imageDest = CGImageDestinationCreateWithData((CFMutableDataRef) imageData, kUTTypeJPEG, 1, NULL); 
CGImageDestinationAddImage(imageDest, [savedImageBitmapRep CGImage], (CFDictionaryRef) properties); 
CGImageDestinationFinalize(imageDest); 

// Do something with imageData 
if (![imageData writeToFile:[@"~/Desktop/test.jpg" stringByExpandingTildeInPath] atomically:NO]) 
    NSLog(@"Failed to write imageData"); 
+0

グレート答えを、あなたはまた、私はカラープロファイルを指定してくださいどのように提案することができますか?たとえばadobe rgb/srgbなど – AmaltasCoder

3
NSImageについては

またはNSImageRepあなたが直接、解像度を設定する代わりに、サイズを設定しないでください。 サイズ、numberOfPixels及び解像度のために、以下の式が成り立つ:

size = numberOfPixels * 72.0/resolution

sizeは長さであり、単位inch/72のドットで表現されます。 (sizeおよびresolutionは浮動小数点数です)。あなたは、dpi = 72のサイズとnumberOfPixelsが数値的に同じである(しかし意味は非常に異なっている)ことがわかります。所望の解像度でNSBitmapImageRepサイズを作成した後

を設定することができます。

NSBitmapImageRep* savedImageBitmapRep = . . . ; // create the new rep 
NSSize newSize; 
newSize.width = [savedImageBitmapRep pixelsWide] * 72.0/resolution; // x-resolution 
newSize.height = [savedImageBitmapRep pixelsHigh] * 72.0/resolution; // y-resolution 
[savedImageBitmapRep setSize:newSize]; 
// save the rep 

二つの発言:あなたは本当にlockFocus/unlockFocusの方法が必要なのでしょうか?新しいNSBitmapImageRepを構築する好ましい方法は、NSGraphicsContextを使用することです。 http://www.mail-archive.com/[email protected]/msg74857.html

NSBitmapImageRepの場合は、TIFFRepresentationを使用するのは非常に時間とスペースを消費します。 10.6以降では、別の ウェイが存在し、コストがかからないため、lockFocusおよびunlockFocusは、フードの下にあるCGImageクラスNSCGImageSnapshotRepのオブジェクトを作成するため、何もありません。 (10.6前にOSのバージョンでは、NSCachedImageRepでした。)以下はそれを行います。

[anImage lockFocus]; 
// draw something 
[anImage unlockFocus]; 
// now anImage contains an NSCGImageSnapshotRep 
CGImageRef cg = [anImage CGImageForProposedRect:NULL context:nil hints:nil]; 
NSBitmapImageRep *newRep = [[NSBitmapImageRep alloc] initWithCGImage:cg]; 
// set the resolution 
// here you may NSLog anImage, cg and newRep 
// save the newRep 
// release the newRep if needed 
関連する問題