5

I am implementing custom keyboard with only images and wanted to send images to textDocumentProxy/input controls like textview on click of image but not get over it. so far I am able to send text/string to input controls but not images.

Is it possible to send images to input controls?

Any suggestion or solution is highly appreciated.

Ajay Gabani
  • 1,168
  • 22
  • 38

2 Answers2

8

Following is the code for the copy image into pasteboard

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://warp.povusers.org/images/test.png"]];
UIPasteboard *pasteBoard=[UIPasteboard generalPasteboard];
[pasteBoard setData:data forPasteboardType:@"public.png"];

Now the think is that create extension of custom keyboard in that layout you can put your UIButton and that button action implement above for png image.

For Local Image following code is help full to you and it's working in my case.

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
NSData *imgData = UIImagePNGRepresentation(@"Your UIImage Here");
[pasteboard setData:imgData forPasteboardType:[UIPasteboardTypeListImage objectAtIndex:0]];

More Uniform Type Identifiers you visit here and change Pasteboard Type.

May this help lot and solve your problem.

Edited

"Allow Full Access" From Settings -> Keyboard -> Keyboards -> select your app keyboard -> On switch of "Allow Full Access"

Nimit Parekh
  • 16,776
  • 8
  • 50
  • 72
  • How do you then paste this image into the textfield the user is currently editing? – Stewart Macdonald Apr 24 '15 at 09:52
  • You just go to any app and on long press option shows paste – Nimit Parekh Apr 24 '15 at 09:53
  • 1
    Righto. So the user has to paste it. I was hoping there was a way to do it automatically. – Stewart Macdonald Apr 24 '15 at 09:59
  • 3
    But one more think you need to give `"Allow Full Access"` From `Settings` -> `Keyboard` -> `Keyboards` -> `select your app keyboard` -> On switch of `"Allow Full Access"`And here my answer to show that option in settings. http://stackoverflow.com/questions/29597700/copying-and-pasting-image-into-a-textbook-in-simulator/29599007#29599007 – Nimit Parekh Apr 24 '15 at 10:05
  • @NimitParekh you should really add that comment to your answer to make it complete. – MSU_Bulldog Aug 11 '16 at 12:38
  • But when i make it true from setting and click on Allow..My app get crashed and showing following message. "Message from debugger: Terminated due to signal 9" And even i am not able to copy and paste image using clipboard. – TBI Nov 11 '16 at 09:40
1

Please try in a device, you can test with iMessage App in a device. Default input views like UITextView and UITextField do not support the images. They only support the strings.

The following code will copy the image into the pasteboard.

let pb = UIPasteboard.generalPasteboard()
let image: UIImage = UIImage(named: imgArray[indexPath.row])!
let imgData: NSData = UIImagePNGRepresentation(image)!
pb.setData(imgData, forPasteboardType: kUTTypePNG as String)

And give "Allow Full Access" for keyboard in settings. And add the RequestsOpenAccess to YES in the info.plist file.

Ramakrishna
  • 712
  • 8
  • 26