使用UIPasteBoard(Swift)复制图像

 宋紫紫云__ 发布于 2022-12-07 17:11

我最近看到了这个项目,用户可以从自定义键盘上点击GIF,他们会看到一个"复制"的工具包.我有一个问题:

如何在产品GIF-Tutorial中重现此工具提示?

任何人都可以给我一些示例代码来使用.我理解如何使用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")

SoftDesigner.. 9

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


user2277872.. 6

尝试使用此代码:

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

你可以在这里找到它的工作原理!

希望这可以帮助

2 个回答
  • 尝试使用此代码: letimage=UIImage(named:myimage.png) UIPasteboard.generalPasteboard().image=image; 你可以在这里找到它的工作原理! 希望这可以帮助
    2022-12-11 01:55 回答
  • 使用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)")
            }
        }
    }
    

    2022-12-11 02:11 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有