ios - 使用 UIPasteBoard 复制图像 (Swift)

标签 ios swift cocoa-touch uipasteboard

我最近看到这个项目,其中用户可以从自定义键盘点击 GIF,然后他们会看到一个“复制的”工具栏。我有一个问题:

任何人都可以给我一些示例代码来使用。我了解如何使用 UIPasteboard 及其功能,但是当我在此函数中输入 UTI 类型“public.png”时,我似乎无法让它工作:(我注意到在 Objective-c 中它是“@public.png ”,但我放置了“public.png”,我无法在网上找到它的来源)

 let imageURL = NSString(string:NSBundle.mainBundle().pathForResource("test", ofType: "png")!)
        var data = NSData(contentsOfURL: NSURL(string:imageURL)!)
        UIPasteboard.generalPasteboard().setData(data!, forPasteboardType: "public.png")

最佳答案

尝试使用此代码:

let image = UIImage(named: "myimage.png")
UIPasteboard.generalPasteboard().image = image;

您可以了解其工作原理 here!

希望对你有帮助

swift 5.1

UIPasteboard.general.image = image

关于ios - 使用 UIPasteBoard 复制图像 (Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27307156/

相关文章:

objective-c - 何时使用 dispatch_once 与重新分配? ( cocoa / cocoa touch )

ios - 如何将标准信息按钮添加到 iOS 中的导航栏?

ios - 如何使用 CaptainHook Hook 协议(protocol)方法

ios - 无法在我的应用程序中打开相机胶卷

ios - 如何将数据从模型数组中的数组填充到 UITableView?

ios - SwiftUI 文本字段文本颜色问题

ios - Storyboard中其他 View Controller 的 IBOutlets

android - asp.net mvc中的Web应用程序与移动应用程序通信

objective-c - 从 saveContext 中的核心数据错误中恢复

iPhone:对象保留其委托(delegate)是否有意义?