12

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 one reproduce this tooltip in the products GIF-Tutorial?

Could anyone give me some sample code to work with. I understand how to use UIPasteboard and it's functions, but I can't seem to get it to work when I put in the UTI type "public.png" in this function: (I noticed in Objective-c it's "@public.png", but I placed "public.png" I couldn't find a source online for this)

 let imageURL = NSString(string:NSBundle.mainBundle().pathForResource("test", ofType: "png")!)
        var data = NSData(contentsOfURL: NSURL(string:imageURL)!)
        UIPasteboard.generalPasteboard().setData(data!, forPasteboardType: "public.png")
Juan Boero
  • 6,281
  • 1
  • 44
  • 62
Guled
  • 659
  • 1
  • 5
  • 18

2 Answers2

16

Try using this code:

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

you can find out how this works here!

Hope this helps

Swift 5.1

UIPasteboard.general.image = image
Jeff
  • 3,829
  • 1
  • 31
  • 49
user2277872
  • 2,963
  • 1
  • 21
  • 22
  • I might need to try it on a physical device. The simulator won't allow me to see my "pasted" image. – Guled Dec 05 '14 at 02:08
  • 1
    That would be a good idea. There are a lot of tools in iOS that require testing on a physical device. – user2277872 Dec 05 '14 at 02:11
9

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)")
        }
    }
}
SoftDesigner
  • 5,640
  • 3
  • 58
  • 47