Swift UIPboard does not copy PNG

My problem is really strange. In the simulator .png copies to the clipboard, and I can paste the image into the Contacts application on the Simulator. But when I put the application on the phone, png is not copied to the clipboard.

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

This is the code that I use, but, as I said, it does not copy to the clipboard. I use this code in the context of the keyboard, although it does not matter when copying to the clipboard. If anyone has any ideas please let me know. Thanks in advance! Oh, this is my first app in Swift and my first app for iOS, so I don't have the experience to find out if this is a Swift problem or something that I just miss. = \

+3
source share
4 answers

Make sure the code works fine in your host application (and not in the keyboard extension application). 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 your keyboard application has nil , it means that you did not provide full access to the keyboard: Copy and paste the image into the tutorial in the simulator

+2
source

I believe that you can use this line to do what you want (you cannot check it right now):

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

Hope this works, I'm a little rusty with a UIP card.

+1
source

There are a lot of errors and problems with the UIPastboard class, so I'm really not surprised that you have problems with something that obviously should work. Honestly, the documentation is not so useful. But try this; this worked for me on a physical device, and it differs from the above methods, which should work, but obviously not 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 "public.jpeg" or "public.png" if the source file is .jpg; he is still working. I think this only changes the format of the nested thing?

Also, were you trying to add the file extension to your first line of code, where do you create the UIImage? This may make it work.

Obviously, the use of this class is temperamental, and not just in this case of use. Therefore, even if we do the same, the difference in this code is only that we create NSData from the path, and not from UIImage. Lol let me know if this works for you.

0
source

Make sure RequestsOpenAccess is set to YES the NSExtension > NSExtensionAttributes in the NSExtensionAttributes extension

0
source

Source: https://habr.com/ru/post/1208396/


All Articles