Copy image using UIPasteBoard (Swift)

I recently saw this project in which the user can use GIFs from the user keyboard, and they would see a โ€œcopiedโ€ toolbar. I have one question:

Can someone give me some sample code to work. I understand how to use UIPastboard, and it works, but I canโ€™t get it to work when I add the public.png type to UTI in this function: (I noticed in "Objective-c it" @ public.png ", but I put "public.png", I could not find a source on the Internet 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") 
+6
source share
3 answers

Try using this code:

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

You can find out how it works here!

Hope this helps

+5
source

When using UIPasteboard.generalPasteboard().image = image; it seems that the image is not copied to cardboard. Instead, try the following code, it will also explain how you can replace the string "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)") } } } 
+9
source

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

0
source

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


All Articles