- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个原型(prototype)来证明从一种格式到另一种格式的 RAW 转换是可能的。我必须将尼康的 .NEF 格式的原始文件转换为佳能的 .CR2 格式。在各种帖子的帮助下,我创建了原始图像 TIFF 表示形式的 BitmapImageRep,并使用它来编写扩展名为 .CR2 的输出文件。
它确实有效,但对我来说唯一的问题是,输入文件为 21.5 MB,但输出为 144.4 MB。使用 NSTIFFCompressionPackBits
时,我得到了 142.1 MB。
我想了解发生了什么,我尝试了各种可用的压缩枚举,但没有成功。
请帮我理解一下。这是源代码:
@interface NSImage(RawConversion)
- (void) saveAsCR2WithName:(NSString*) fileName;
@end
@implementation NSImage(RawConversion)
- (void) saveAsCR2WithName:(NSString*) fileName
{
// Cache the reduced image
NSData *imageData = [self TIFFRepresentation];
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
// http://www.cocoabuilder.com/archive/cocoa/151789-nsbitmapimagerep-compressed-tiff-large-files.html
NSDictionary *imageProps = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:NSTIFFCompressionJPEG],NSImageCompressionMethod,
[NSNumber numberWithFloat: 1.0], NSImageCompressionFactor,
nil];
imageData = [imageRep representationUsingType:NSTIFFFileType properties:imageProps];
[imageData writeToFile:fileName atomically:NO];
}
@end
如何获得 CR2 格式的输出文件,但其大小几乎与输入文件大小相同,且几乎没有 CR2 文件所需的变化?
编辑 1:根据 Peter 使用 CGImageDestinationAddImageFromSource 方法的建议完成了更改,但我仍然得到相同的结果。输入源 NEF 文件大小为 21.5 MB,但转换后目标文件大小为 144.4 MB。
请检查代码:
-(void)saveAsCR2WithCGImageMethodUsingName:(NSString*)inDestinationfileName withSourceFile:(NSString*)inSourceFileName
{
CGImageSourceRef sourceFile = MyCreateCGImageSourceRefFromFile(inSourceFileName);
CGImageDestinationRef destinationFile = createCGImageDestinationRefFromFile(inDestinationfileName);
CGImageDestinationAddImageFromSource(destinationFile, sourceFile, 0, NULL);
//https://developer.apple.com/library/mac/#documentation/graphicsimaging/Conceptual/ImageIOGuide/ikpg_dest/ikpg_dest.html
CGImageDestinationFinalize(destinationFile);
}
CGImageSourceRef MyCreateCGImageSourceRefFromFile (NSString* path)
{
// Get the URL for the pathname passed to the function.
NSURL *url = [NSURL fileURLWithPath:path];
CGImageSourceRef myImageSource;
CFDictionaryRef myOptions = NULL;
CFStringRef myKeys[2];
CFTypeRef myValues[2];
// Set up options if you want them. The options here are for
// caching the image in a decoded form and for using floating-point
// values if the image format supports them.
myKeys[0] = kCGImageSourceShouldCache;
myValues[0] = (CFTypeRef)kCFBooleanTrue;
myKeys[1] = kCGImageSourceShouldAllowFloat;
myValues[1] = (CFTypeRef)kCFBooleanTrue;
// Create the dictionary
myOptions = CFDictionaryCreate(NULL, (const void **) myKeys,
(const void **) myValues, 2,
&kCFTypeDictionaryKeyCallBacks,
& kCFTypeDictionaryValueCallBacks);
// Create an image source from the URL.
myImageSource = CGImageSourceCreateWithURL((CFURLRef)url, myOptions);
CFRelease(myOptions);
// Make sure the image source exists before continuing
if (myImageSource == NULL){
fprintf(stderr, "Image source is NULL.");
return NULL;
}
return myImageSource;
}
CGImageDestinationRef createCGImageDestinationRefFromFile (NSString *path)
{
NSURL *url = [NSURL fileURLWithPath:path];
CGImageDestinationRef myImageDestination;
//https://developer.apple.com/library/mac/#documentation/graphicsimaging/Conceptual/ImageIOGuide/ikpg_dest/ikpg_dest.html
float compression = 1.0; // Lossless compression if available.
int orientation = 4; // Origin is at bottom, left.
CFStringRef myKeys[3];
CFTypeRef myValues[3];
CFDictionaryRef myOptions = NULL;
myKeys[0] = kCGImagePropertyOrientation;
myValues[0] = CFNumberCreate(NULL, kCFNumberIntType, &orientation);
myKeys[1] = kCGImagePropertyHasAlpha;
myValues[1] = kCFBooleanTrue;
myKeys[2] = kCGImageDestinationLossyCompressionQuality;
myValues[2] = CFNumberCreate(NULL, kCFNumberFloatType, &compression);
myOptions = CFDictionaryCreate( NULL, (const void **)myKeys, (const void **)myValues, 3,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
//https://developer.apple.com/library/mac/#documentation/graphicsimaging/Conceptual/ImageIOGuide/imageio_basics/ikpg_basics.html#//apple_ref/doc/uid/TP40005462-CH216-SW3
CFStringRef destFileType = CFSTR("public.tiff");
// CFStringRef destFileType = kUTTypeJPEG;
CFArrayRef types = CGImageDestinationCopyTypeIdentifiers(); CFShow(types);
myImageDestination = CGImageDestinationCreateWithURL((CFURLRef)url, destFileType, 1, myOptions);
return myImageDestination;
}
编辑2:使用@Peter告诉的第二种方法。这给出了有趣的结果。其效果与在查找器中将文件重命名为“example_image.NEF”至“example_image.CR2”相同。令人惊讶的是,当以编程方式和在 finder 中进行转换时,21.5 MB 的源文件将变成 59 KB。代码中没有任何压缩集。请查看代码并提出建议:
-(void)convertNEFWithTiffIntermediate:(NSString*)inNEFFile toCR2:(NSString*)inCR2File
{
NSData *fileData = [[NSData alloc] initWithContentsOfFile:inNEFFile];
if (fileData)
{
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:fileData];
// [imageRep setCompression:NSTIFFCompressionNone
// factor:1.0];
NSDictionary *imageProps = nil;
NSData *destinationImageData = [imageRep representationUsingType:NSTIFFFileType properties:imageProps];
[destinationImageData writeToFile:inCR2File atomically:NO];
}
}
最佳答案
我要尝试的第一件事根本不涉及 NSImage 或 NSBitmapImageRep。相反,我会为源文件创建一个 CGImageSource,为目标文件创建一个 CGImageDestination,并使用 CGImageDestinationAddImageFromSource
将所有图像从 A 传输到 B。
关于cocoa - NSImage + NSBitmapImageRep = 将 RAW 图像文件从一种格式转换为另一种格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15458566/
我有一个矩形 NSImage A,我想缩放以嵌入到方形透明图像 B 中,保持 A 的比例。所以,最后我会得到一个带有矩形的方形图像。 我该如何构图?我的意思是,如何在另一个 NSImage 上绘制一个
我有一堆 NSImages,我将它们保存在 PDFDocument 中,作为 PDFPages。当我第一次插入 NSImage 时,NSImage 的大小以点 (72 dpi) 为单位,而(仅)NSI
我试图理解为什么 var tmp: Any = NSImage(byReferencingFile: somePath) var img: NSImage = tmp as NSImage 不会工作并
好吧,我很困惑,因为这应该很简单。 在 Xcode 5 中,我开始了一个新的 iphone 项目。在 ViewDidLoad 中,我开始输入: NSImage *图像 ... 实际上在“NSI”点……
我正在尝试将文件从 xcassets 加载到 NSImage,其中 Assets 名称是 Logo 。 我试过这个: let logoIcon = NSImage(named: "logo") 但我一
我正在编写一个简单的 MacRuby 应用程序。我有一个 NSView,它在调用 drawRect() 时在自身内部绘制图像。 在那个 NSView 子类中,我的 drawRect() 有(以及其他一
所以我有一个 NSImage *startingImage 它由带有灰色色彩空间的 NSBitmapImageRep 表示 我需要反转它的颜色,因此我将其转换为 CIImage CIImage *
首先,我知道我不应该使用 SQLite 数据库来存储图像,但我只存储非常小的网站图标。 我的问题是我尝试将这些图标插入数据库(似乎有效),我将图标转换为 NSData与 -tiffrepresenta
我正在尝试制作一个简单的基于文档的 cocoa 应用程序,可以在 pdf 文件中保存和加载图像。 对于保存部分,我正在使用 - (NSData *)dataOfType:(NSString *)typ
我使用 NSAffineTransform 类在其中心旋转了 NSImage,它可以工作。代码在这里: - (NSImage*)rotateImage: (NSImage*)myImage angle
我是 cocoa 和编程的新手,如果这是基本的东西,我很抱歉。 我正在使用 ARC。我有一个 NSImageView 组件,它由 DropZone 类控制。我将图像拖放到其中,但是一旦我尝试调用我得到
我想创建 NSScrollView 对象的 NSImage,这样我就可以将平面图形用于动画目的。 当我将 ScrollView 对象渲染为图形并将其添加回我的窗口时,它可以工作,但看起来非常糟糕,就像
到目前为止,我已经构建了一个应用程序,允许用户将图像拖放到 NSImageView 上。但是,我希望能够通过简单地单击任何图像并按住鼠标按钮来移动其位置来移动这些图像。 设置图像后如何操作 NSIma
我正在尝试打印 NSImage,但无法将其调整为正确的尺寸。我希望 NSImage 在任何足够大的纸张上打印为 4"x6"照片。这是我现在的代码。 var printView = NSImageVie
这可能是一个新问题,但我无法理解如何操作 NSImage 实例。 我正在尝试创建一个方法,该方法将采用 NSImage (一个图标),在其上绘制另一个 NSImage (复选标记),然后返回修改后的
由于 Cocoa-Java 已弃用,我正在将 Cocoa-Java 代码迁移到 Cocoa + JNI。该代码打印存储在文件中的图像。新的 Cocoa 代码基本上是: NSImage *image =
我正在尝试设置一个自定义拖动图标以在 NSTableView 中使用。一切似乎都正常,但由于我对 Quartz 缺乏经验,我遇到了问题。 - (NSImage *)dragImageForRowsWi
我有许多 NSImage,正在自定义 NSView 子类中渲染。当用户将鼠标放在这些图像上时,我希望放大这些图像,并且想知道执行此操作的最佳方法。 目前我正在使用 NSView 的 drawRect:
我的程序中有一个大型一维动态数组,它表示磁盘上的 FITS 图像,即它保存图像的所有像素值。数组的类型是double。目前,我只关心单色图像。 由于 Cocoa 不直接支持 FITS 格式,因此我使用
我想获取 NSImage.h 中定义的常量的图像文件,例如 NSImageNameGoRightTemplate 。我想复制并编辑其中一些。有谁知道这些图像位于哪里?我太笨了,在驱动器上找不到它们..
我是一名优秀的程序员,十分优秀!