【问题标题】:NSPasteBoard _setData:forType NSImage fails for PNG fileNSPasteBoard _setData:forType NSImage 对 PNG 文件失败
【发布时间】:2017-10-25 09:02:58
【问题描述】:

我正在使用code from this site 将图像文件复制到剪贴板。这是完整的源代码

#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import <unistd.h>
BOOL copy_to_clipboard(NSString *path)
{
  // http://stackoverflow.com/questions/2681630/how-to-read-png-image-to-nsimage
  NSImage * image;
  if([path isEqualToString:@"-"])
  {
    // http://caiustheory.com/read-standard-input-using-objective-c 
    NSFileHandle *input = [NSFileHandle fileHandleWithStandardInput];
    image = [[NSImage alloc] initWithData:[input readDataToEndOfFile]];
  }else
  { 
    image =  [[NSImage alloc] initWithContentsOfFile:path];
  }
  // http://stackoverflow.com/a/18124824/148668
  BOOL copied = false;
  if (image != nil)
  {
    NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
    [pasteboard clearContents];
    NSArray *copiedObjects = [NSArray arrayWithObject:image];
    copied = [pasteboard writeObjects:copiedObjects];
    [pasteboard release];
  }
  [image release];
  return copied;
}

int main(int argc, char * const argv[])
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  if(argc<2)
  {
    printf("Usage:\n\n"
      "Copy file to clipboard:\n    ./impbcopy path/to/file\n\n"
      "Copy stdin to clipboard:\n    cat /path/to/file | ./impbcopy -");
    return EXIT_FAILURE;
  }
  NSString *path= [NSString stringWithUTF8String:argv[1]];
  BOOL success = copy_to_clipboard(path);
  [pool release];
  return (success?EXIT_SUCCESS:EXIT_FAILURE);
}

当我使用 PNG 文件运行编译后的二进制文件时,出现此错误

$ ~/bin/imgbcopy prof/combined.png 
2017-10-25 16:24:50.373 imgbcopy[80618:4292276] -[NSPasteBoard _setData:forType:index:usesPboardTypes:] returns false. Type: public.tiff, index: 0 class: NSImage.

从 bash 管道复制 PNG 图像也会失败

$ cat prof/combined.png | ~/bin/imgbcopy -
2017-10-25 16:27:52.856 imgbcopy[80690:4293881] -[NSPasteBoard _setData:forType:index:usesPboardTypes:] returns false. Type: public.tiff, index: 0 class: NSImage.

用它测试另一个随机的 PNG 屏幕截图效果很好。我注意到上面的错误消息说Type: public.tiff。 PNG 最初是使用 ImageMagic 从 SVG 转换而来的。

代码有什么问题,还是格式错误的 PNG?

PNG File in question

【问题讨论】:

    标签: objective-c macos png


    【解决方案1】:

    我做了一些测试,结果非常显着。

    第一次测试:

    NSData *imageData = [NSData dataWithContentsOfFile:[@"~/Downloads/combined.png" stringByExpandingTildeInPath]];
    NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
    [pasteBoard clearContents];
    
    NSPasteboardItem *pasteboardItem = [[NSPasteboardItem alloc] init];
    [pasteboardItem setData:imageData forType:NSPasteboardTypePNG];
    
    NSLog(@"Write result: %i",  [pasteBoard writeObjects:@[pasteboardItem]]);
    
    NSImage *image = [[NSImage alloc] initWithContentsOfFile:[@"~/Downloads/combined.png" stringByExpandingTildeInPath]];
    NSUInteger length = [[image TIFFRepresentation] length];
    NSLog(@"%f MB (%lu)", length / 1000.0 / 1000.0, length);
    
    [pasteBoard writeObjects:@[image]];
    

    输出:

    2017-11-30 15:57:53.317349+0100 Lolo[4927:639480] Write result: 1
    2017-11-30 15:57:54.831260+0100 Lolo[4927:639480] 347.824566 MB (347824566)
    2017-11-30 15:57:55.293900+0100 Lolo[4927:639480] -[NSPasteBoard _setData:forType:index:usesPboardTypes:] returns false. Type: public.tiff, index: 1 class: NSImage.
    

    我可以将图像作为 PNG 数据写入粘贴板。不能像您的情况那样编写 NSImage 对象。 NSImage 对象以多种格式存储自己,包括 TIFF 数据,所以只是为了测试我打印它将写入粘贴板的图像数据的大小。它是 347.824566 MB,所以可能有点大。

    第二次测试:

    NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
    NSData *data = [pasteBoard dataForType:NSPasteboardTypeTIFF];
    float length = [data length];
    NSLog(@"%f MB (%f) - %@ - %@", length / 1000.0 / 1000.0, length, data, [pasteBoard types]);
    

    输出:

    2017-11-30 15:49:51.018708+0100 Lolo[4786:624058] 0.000000 MB (0.000000) - (null) - (
        "public.tiff",
        "NeXT TIFF v4.0 pasteboard type",
        "dyn.ah62d4rv4gu8zazwuqm10c6xemf1gq54uqm10c6xenv61a3k",
        PVPboardInfoPboardType
    )
    

    在第二个测试中,我在预览中打开了图像。我复制了图像,没有错误。检查 Finder 中的剪贴板(粘贴板)说它有一个 TIFF 图像,但什么也没显示。使用粘贴板代码打印内容,显示粘贴板包含 TIFF 类型,但没有数据。

    Mac OS (macOS) 似乎根本无法存储与使用的图像一样大的图像。

    【讨论】:

      猜你喜欢
      • 2018-08-10
      • 1970-01-01
      • 1970-01-01
      • 2013-12-25
      • 2013-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-17
      相关资源
      最近更新 更多