【问题标题】:Copy Image with UIPasteBoard (Swift)使用 UIPasteBoard (Swift) 复制图像
【发布时间】:2015-02-03 02:35:03
【问题描述】:

我最近看到了这个项目,其中用户可以从自定义键盘点击 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")

【问题讨论】:

    标签: ios swift cocoa-touch uipasteboard


    【解决方案1】:

    尝试使用此代码:

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

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

    希望对你有帮助

    斯威夫特 5.1

    UIPasteboard.general.image = image
    

    【讨论】:

    • 我可能需要在物理设备上尝试一下。模拟器不允许我看到我的“粘贴”图像。
    • 那是个好主意。 iOS 中有很多工具需要在物理设备上进行测试。
    【解决方案2】:

    使用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)")
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-01
      • 2012-02-10
      • 1970-01-01
      • 2012-09-18
      • 1970-01-01
      • 2018-01-28
      • 2012-10-28
      相关资源
      最近更新 更多