2012-02-10 63 views
2

我有一個可以讓用戶導出JPEG圖像的Cocoa Mac圖像編輯應用程序。我目前使用如下代碼到這些圖像導出爲JPEG文件:如何在Cocoa應用程序中設置導出的JPEG圖像的每英寸像素數?

//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]; 

不過,我想用戶能夠爲每英寸的像素爲這個JPEG圖像(像您可以在Photoshop中的導出選項)。在上面的代碼中,我需要修改什麼來調整導出的JPEG的值?

回答

2

我找不到使用NSImage API的方法,但CGImage可以通過設置kCGImagePropertyDPIHeight/Width來實現。

我還設置了kCGImageDestinationLossyCompressionQuality,我認爲它與NSImageCompressionFactor相同。

//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 2012-02-11 12:05:26

3

對於NSImageNSImageRep你不直接設置分辨率,但設置的大小來代替。 對於大小,numberOfPixels和分辨率下式成立:

size = numberOfPixels * 72.0/resolution

size是一個長度和在點與單元inch/72表示。 (sizeresolution是漂浮物)。您可以看到,對於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

而且:使用TIFFRepresentation作爲NSBitmapImageRep是非常耗費時間和空間的。由於10.6有另一種方式存在,並且不需要任何費用,因爲lockFocus和unlockFocus創建了一個類NSCGImageSnapshotRep的對象,它的底層是一個CGImage。 (在10.6之前的操作系統版本,這是一個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 
相關問題