Ios 使用UIPasteBoard(Swift)复制图像

Ios 使用UIPasteBoard(Swift)复制图像,ios,swift,cocoa-touch,uipasteboard,Ios,Swift,Cocoa Touch,Uipasteboard,我最近看到了这个项目,在这个项目中,用户可以从自定义键盘点击GIF,他们会看到一个“复制”的toolip出现。我有一个问题: 如何在产品中重现此工具提示 谁能给我一些示例代码来使用。我知道如何使用UIPasteboard及其函数,但当我在这个函数中输入UTI类型“public.png”时,我似乎无法让它工作:(我注意到在Objective-c中它是“@public.png”,但我将“public.png”放在网上,我找不到这方面的源代码) 尝试使用以下代码: let image = UIIm

我最近看到了这个项目,在这个项目中,用户可以从自定义键盘点击GIF,他们会看到一个“复制”的toolip出现。我有一个问题:

  • 如何在产品中重现此工具提示
谁能给我一些示例代码来使用。我知道如何使用UIPasteboard及其函数,但当我在这个函数中输入UTI类型“public.png”时,我似乎无法让它工作:(我注意到在Objective-c中它是“@public.png”,但我将“public.png”放在网上,我找不到这方面的源代码)

尝试使用以下代码:

let image = UIImage(named: "myimage.png")
UIPasteboard.generalPasteboard().image = image;
您可以了解它是如何工作的

希望这有帮助

Swift 5.1

UIPasteboard.general.image = image

使用
UIPasteboard.generalPasteboard()时,image=image图像似乎没有复制到粘贴板。请尝试下一个代码,它还解释了如何替换
“public.png”
字符串:

// The Pasteboard is nil if full access is not granted
// 'image' is the UIImage you about to copy to the pasteboard
if let pb = UIPasteboard.generalPasteboard() {
    let type = UIPasteboardTypeListImage[0] as! String
    if !type.isEmpty {
        pb.setData(UIImagePNGRepresentation(image), forPasteboardType: type)
        if let readData = pb.dataForPasteboardType(type) {
            let readImage = UIImage(data: readData, scale: 2)
            println("\(image) == \(pb.image) == \(readImage)")
        }
    }
}

我可能需要在物理设备上试用。模拟器不允许我看到“粘贴”的图像。这是个好主意。iOS中有许多工具需要在物理设备上进行测试。
// The Pasteboard is nil if full access is not granted
// 'image' is the UIImage you about to copy to the pasteboard
if let pb = UIPasteboard.generalPasteboard() {
    let type = UIPasteboardTypeListImage[0] as! String
    if !type.isEmpty {
        pb.setData(UIImagePNGRepresentation(image), forPasteboardType: type)
        if let readData = pb.dataForPasteboardType(type) {
            let readImage = UIImage(data: readData, scale: 2)
            println("\(image) == \(pb.image) == \(readImage)")
        }
    }
}