Copy Image with UIPasteBoard (Swift)

后端 未结 2 852
星月不相逢
星月不相逢 2021-02-04 19:15

I recently saw this project in which a user can tap on a GIF from a custom keyboard and they would see a \"copied\" toolip appear. I have one question:

  • How does on
2条回答
  •  梦如初夏
    2021-02-04 19:37

    When using UIPasteboard.generalPasteboard().image = image; it seems the image is not copied to the pasteboard. Instead try the next code, it also explains how you can replace "public.png" string:

    // 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)")
            }
        }
    }
    

提交回复
热议问题