3

我想要做的是将我的应用程序中的一些图像粘贴到 SMS 中。

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSString *imagefile = [[NSBundle mainBundle] 
                       pathForResource:@"imagename"]
                       ofType:@"png"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:imagefile];

if (fileExists){    
    UIImage *ui = [[UIImage alloc] initWithContentsOfFile:imagefile];
    pasteboard.image = ui;
    [ui release];
}

在调试模式下,我发现图像确实存在,并且它确实进入了粘贴板(我检查了它我引入了一个带有粘贴板图像的图像视图,这是必要的)。

保存到剪贴板后,我打电话

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:"]];

它确实弹出,但是当我点击那里的“文本字段”时,没有显示粘贴按钮。有人可以指出我的错误吗? 或者这样做有意义吗?我的意思是,是否可以通过默认 iPhone 消息应用程序发送图像?

4

3 回答 3

3

只有当粘贴板包含您点击的当前对象(此处为文本字段)支持的项目时,才会显示“粘贴”操作。您似乎只将图像添加到粘贴板。文本字段不支持图像。所以“粘贴”动作不会出现。

于 2011-04-14T13:13:21.077 回答
1

我有这个工作。我只是使用 setData 为其提供原始数据,然后使用 forPasteboardType 设置数据类型。就在您的下方

    if (fileExists){

试试这个

    NSData *data = [NSData dataWithContentsOfFile:imagefile];
    [pasteboard setData:data forPasteboardType:@"public.png"];            

您可以在此处查找不同的 PasteboardType UTI

于 2012-03-20T20:08:14.870 回答
1

此代码工作正常:

UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"imageName"]];
[[UIPasteboard generalPasteboard] setImage:image];
于 2014-11-11T07:34:30.040 回答