2

经过一整天的搜索,我找到了一些类似的答案,但似乎没有什么对我有用。

我已经构建了一个要在 OSX 中使用的应用程序,它从用户输入的目录中获取图像列表,通过重命名过程运行每个图像,转换为 JPG,并希望在您的帮助下转换为 RGB 颜色。

NSImage *image = [[NSImage alloc] initWithContentsOfFile:from];

NSInteger imageWidth = image.size.width * 4.16666666667;
NSInteger imageHeight = image.size.height * 4.16666666667;

NSBitmapImageRep* theRGBImageRep = [[NSBitmapImageRep alloc]
    initWithBitmapDataPlanes:NULL pixelsWide:imageWidth pixelsHigh:imageHeight
        bitsPerSample: 8 samplesPerPixel: 4 hasAlpha: YES isPlanar: NO
    colorSpaceName: NSCalibratedRGBColorSpace bytesPerRow: 0 bitsPerPixel: 0];

NSGraphicsContext* theContext = [NSGraphicsContext
                    graphicsContextWithBitmapImageRep: theRGBImageRep];

NSImageRep* theImageRep = [image bestRepresentationForRect:
   NSMakeRect(0.0, 0.0, imageWidth, imageHeight) context:theContext
     hints:[NSDictionary dictionaryWithObject:NSDeviceRGBColorSpace
                     forKey:NSDeviceColorSpaceName]];

[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext: theContext];

NSRect theBoundsRect = NSMakeRect(0.0, 0.0, [theRGBImageRep pixelsWide],
                                                [theRGBImageRep pixelsHigh]);

[[NSColor clearColor] set];

NSRectFill(theBoundsRect);

[theImageRep drawInRect: theBoundsRect];
[NSGraphicsContext restoreGraphicsState];;
[image addRepresentation: theRGBImageRep];

NSDictionary *imageProps = [NSDictionary dictionaryWithObjectsAndKeys:
        [NSNumber numberWithFloat:1.0],NSImageCompressionFactor, nil];
NSData *bitmapData = [theRGBImageRep representationUsingType:NSJPEGFileType
                   properties:imageProps];

if ([bitmapData writeToFile:toJPG atomically:YES] == YES){
    feedback = [NSString stringWithFormat:@"%@ -> %@\n", string, jpgFileName];
    [readOut insertText:feedback];
    [readOut display];
} else {
    feedback = [NSString stringWithFormat:@"%@ - FAILED\n", string];
    [errorLog insertText:feedback];
    [errorLog display];
}

如果图像在指定文件夹中正确输出为 JPEG,但始终保持 CMYK。还有其他人遇到这个吗?

已编辑以显示已清除的结果:

更新的代码(这是当前给出的淘汰结果)

4

1 回答 1

1

试试这个

NSImageRep* theImageRep = [inputImage bestRepresentationForDevice:
[NSDictionary dictionaryWithObject: NSDeviceRGBColorSpace

forKey: NSDeviceColorSpaceName]];

NSString* theColorSpace = [theImageRep colorSpaceName];

if ([theColorSpace isEqualToString: NSDeviceCMYKColorSpace])
{
NSBitmapImageRep* theRGBImageRep = [[[NSBitmapImageRep alloc]
initWithBitmapDataPlanes: NULL

pixelsWide: [theImageRep pixelsWide]

pixelsHigh: [theImageRep pixelsHigh]

bitsPerSample: 8

samplesPerPixel: 4

  hasAlpha: YES

  isPlanar: NO

 colorSpaceName: NSDeviceRGBColorSpace

 bytesPerRow: 0

 bitsPerPixel: 0] autorelease];


  NSGraphicsContext* theContext = [NSGraphicsContext
graphicsContextWithBitmapImageRep: theRGBImageRep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext: theContext];

NSRect theBoundsRect = NSMakeRect(0.0, 0.0, [theRGBImageRep pixelsWide],
[theRGBImageRep pixelsHigh]);

[[NSColor clearColor] set];
NSRectFill(theBoundsRect);

[theImageRep drawInRect: theBoundsRect];

[NSGraphicsContext restoreGraphicsState];


ASSIGNOBJECT(rgbImage, [[[NSImage alloc] initWithSize: [theImageRepsize]] autorelease]);

[rgbImage addRepresentation: theRGBImageRep];
}
else
{
ASSIGNOBJECT(rgbImage, inputImage);
 }
于 2013-01-23T18:22:52.903 回答