2017-10-25 9 views
0

code from this siteを使用して、画像ファイルをクリップボードにコピーしています。これは私がPNGファイルでコンパイルされたバイナリを実行すると、私はこのエラーNSPasteBoard _setData:forType PNGファイルのNSImageが失敗する

$ ~/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. 
を失敗したコピーを取得し、完全なソースコード

#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スクリーンショットでうまく動作するかどうかをテストします。上記のエラーメッセージにはType: public.tiffと表示されています。 PNGはImageMagicを使用してSVGから最初に変換されました。

コードの問題は何ですか?または不正な形式のPNGですか?

PNG File in question

答えて

0

私はいくつかのテストを行い、結果は非常に顕著です。

まずテスト:

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 
) 

Iプレビュー画像を開いた第二の試験では。画像をコピーしましたが、エラーはありません。 Finderでクリップボード(ペーストボード)を確認すると、TIFF画像が表示されますが、何も表示されません。ペーストボードコードを使用して内容を印刷すると、ペーストボードにはTIFFタイプが含まれていてデータは含まれていないことがわかります。

MacOS(macOS)は、使用されているものほど大きな画像を保存できないようです。

関連する問題