3

My problem is really odd. In the simulator the .png copies to clipboard fine and I can paste the image in the Contacts app on Simulator. But when I put the app on the phone, the png is not copied to clipboard.

let img = UIImage(named: "myimage")
let data = NSData(data: UIImagePNGRepresentation(img) )
UIPasteboard.generalPasteboard().setData(data, forPasteboardType: "public.png")

That's the code I'm using but like I said it does not copy to the clipboard. I'm using this code within the context of a keyboard, although that shouldn't matter when copying to a clipboard. If anyone has any ideas please let me know. Thanks in advance! Oh this is my first app in Swift and my first iOS app, so I don't have the seasoned experience to know if this is a Swift issue or something I'm just missing. =\

4 Answers4

2

Make sure the code runs fine in your host app (not a keyboard extension app). For example, check if the read image has the same resolution:

    //the Pasteboard is nil if full access is not granted
    let pbWrapped: UIPasteboard? = UIPasteboard.generalPasteboard()
    if let pb = pbWrapped {
        var type = UIPasteboardTypeListImage[0] as! String
        if (count(type) > 0) && (image != nil) {
            pb.setData(UIImagePNGRepresentation(image), forPasteboardType: type)
            var readDataWrapped: NSData? = pb.dataForPasteboardType(type)
            if let readData = readDataWrapped {
                var readImage = UIImage(data: readData, scale: 2)
                println("\(image) == \(pb.image) == \(readImage)")
            }
        }
    }

If the pasteboard object is nil in your keyboard app that means you haven't provided full access to the keyboard: Copying and pasting image into a textbook in simulator

Community
  • 1
  • 1
SoftDesigner
  • 5,640
  • 3
  • 58
  • 47
  • I have give full access from settings. I can paste text in "Message" application. But Can't paste sticker image. While I can paste sticker image in "Note" application! any idea? – kb920 Jan 05 '16 at 13:44
  • 1
    kb290: make sure you're pasring an image to the iMessage chat. You cannot send images via SMS therefore pasting is disabled for such chats. – SoftDesigner Jan 06 '16 at 08:36
  • 1
    I got solution from your answer...:) http://stackoverflow.com/questions/27307156/copy-image-with-uipasteboard-swift/30226140#comment56999470_30226140 – kb920 Jan 06 '16 at 09:05
1

I believe you can use this line to do what you want (not able to test it out right now):

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

Hopefully that works, I'm a little rusty with UIPasteboard.

erdekhayser
  • 6,537
  • 2
  • 37
  • 69
  • I tried what you just typed. But unfortunately, when pasting it just pastes the last thing that was in the iOS clipboard. Is there a way to clear the clipboard before copying to it? – Mr. Bloodhound Sep 18 '14 at 13:24
  • 1
    If you can verify that it works in Swift that would be great because I've tried it so many different ways and still nothing. – Mr. Bloodhound Sep 18 '14 at 14:09
  • This should work, "Setting this property replaces all current items in the pasteboard with the new item." – David Berry Sep 18 '14 at 15:17
  • 1
    Yes David lol I know it should work but the fact is that for some reason it doesn't work ^_^ – Mr. Bloodhound Sep 19 '14 at 00:44
  • Hey Mr. Bloodhound, haha sounds so formal, the fact that you're getting the last thing that was copied to the clipboard (and not the thing that you copied), I think it means that NSData creation from your UIImage failed perhaps for lack of an extension name when you made the UIImage. So perhaps David was right in saying that your data may have been nil. – Peter Parker Jul 17 '16 at 15:40
  • Is there a way to determine if it was copied or not? so as to show the user with a alert that it was copied? – bhakti123 Feb 07 '17 at 12:16
0

There are lots of bugs and issues with the UIPasteboard class, so I'm really not surprised that you're having issues with something that so obviously is supposed to work. The documentation isn't that helpful either, to be honest. But try this; this worked for me on a physical device, and it's different to the above methods that are supposed to work but evidently don't for a bunch of people.

guard let imagePath = NSBundle.mainBundle().pathForResource("OliviaWilde", ofType: "jpg") else
{ return }

guard let imageData = NSData(contentsOfFile: imagePath) else { return }

let pasteboard = UIPasteboard.generalPasteboard()

pasteboard.setData(imageData, forPasteboardType: "public.jpeg")

You can use either "public.jpeg" or "public.png" if the source file is .jpg; it still works. I think it only changes the format of the thing that gets pasted?

Also, did you try adding the file extension in your first line of code where you create the UIImage? That might make it work too.

Evidently use of this class is temperamental, not just in this use case. So even though we're doing same thing, only difference in this code is we're creating the NSData from a path rather than a UIImage. Lol let me know if that works for you.

Peter Parker
  • 2,121
  • 1
  • 17
  • 23
0

Ensure that RequestsOpenAccess is set to YES under NSExtension > NSExtensionAttributes in the extension's info.plist

SINDHYA PETER
  • 965
  • 9
  • 35