将图像复制到粘贴板将jpeg转码为png

时间:2011-04-03 00:53:55

标签: objective-c iphone-sdk-3.0 copy-paste uipasteboard

我正在使用iPhone应用程序在jpeg文件中嵌入/提取数据。我想让用户能够将生成的图像复制到剪贴板,但是我正在使用的代码将生成的jpeg转换为png,当它被复制到剪贴板时。

我正在使用下面的代码,有什么我可以做的,以确保它是一点一滴地复制和粘贴jpeg?

// copy to clipboard
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.image = [UIImage imageNamed:@"output.jpg"];

提前致谢!

2 个答案:

答案 0 :(得分:5)

我终于想出了这个。

// copy to clipboard
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSData *data = [NSData dataWithContentsOfFile:filePath];
[pasteboard setData:data forPasteboardType:@"public.jpeg"];

...

// copy from clipboard
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSData *data = [pasteboard dataForPasteboardType:@"public.jpeg"];
NSString  *copyPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/image.jpg"];
[data writeToFile:copyPath atomically:YES];

答案 1 :(得分:-1)

NSPasteboard *pboard  = [NSPasteboard generalPasteboard];
[pboard declareTypes: [NSMutableArray arrayWithObject:
                       NSTIFFPboardType] owner: nil];
[pboard setData:[imgView.image TIFFRepresentation] forType:NSTIFFPboardType];

NSData *data = [[NSPasteboard generalPasteboard] dataForType:NSPasteboardTypeTIFF];
    if (data) {
           // Do your stuff here 
    }

完全正常工作的代码,我使用相同的代码

祝你好运!!!!