2011-04-03 77 views
5

我正在研究iPhone應用程序以將數據嵌入/提取爲jpeg文件。我想讓用戶能夠將生成的圖像複製到剪貼板,但是我使用的代碼將生成的jpeg複製到剪貼板時將其轉換爲png。複製圖像到粘貼板將jpeg轉碼爲png

我正在使用下面的代碼,有什麼我可以做,以確保它是一點點複製和jpeg粘貼?

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

在此先感謝!

回答

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]; 
+0

嘿,本,我也是這樣使用的,但是我發現無論從紙板上拉下圖像,它都會變成png。思考? – 2012-01-28 19:30:45

+0

對不起,我錯過了這個評論...無論如何,你怎麼把它從剪貼板上拉下來?我從來沒有遇到任何問題。當我找到我的代碼時,我可以發佈代碼以稍後檢索它。 – 2012-05-24 17:07:31

+0

嗨,本。這是我們試圖推送JPG的代碼。我認爲它確實如此,但它似乎總是以png形式回到電子郵件和iMessage中。 UIImage * jpeg = [UIImage imageWithData:UIImageJPEGRepresentation(img,0.2)]; [[UIPasteboard generalPasteboard] setValue:[UIImage imageWithData:UIImageJPEGRepresentation(img,0.5)] forPasteboardType:@「public.jpeg」]; – 2012-08-28 16:41:38

-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 
    } 

完全工作代碼,I M使用相同的代碼

好運!!!!

+0

這個問題是針對iOS的。您的代碼適用於OS X. – ThomasW 2014-10-10 02:08:10

+0

更不用說要使用JPEG而不是TIFF的問題了。 – bhaller 2017-11-08 10:37:51