How to send image using iOS custom keyboard

I am creating my first custom keyboard. I use Swift 2 and Xcode 7. I have it like my keyboard

Keyboard

(I run it on my iPhone) When I click on a small foreign face, I would like to have

  • a little emoji with this image or

  • paste the image (if possible) to where the user is typing. I tried this code

    let pasteboard: UIPasteboard = UIPasteboard.generalPasteboard()
    let image: UIImage = currentImage!
    let newImage = scaleImage(image, toSize: CGSize(width: 40, height: 40))
    let imgData: NSData = UIImagePNGRepresentation(newImage)!
    pasteboard.setData(imgData, forPasteboardType: UIPasteboardTypeListImage[0] as! String)
    
    let proxy = UITextDocumentProxy.self as! UITextDocumentProxy
    let data = pasteboard.string!
    print(data)
    proxy.insertText(data)
    

but I did not succeed. When I print(data), I get nil, and then EXC_BAD_ACCESSon the next line. How can I achieve one of the two goals that I had? Thank you for your help.

+4
source share
1 answer

For Swift 3:

import MobileCoreServices

let pasteboard: UIPasteboard = UIPasteboard.general
let imageName = "yourImage.png"
let newImage = UIImage(named: imageName)
let imgData = UIImagePNGRepresentation(newImage!)!
pasteboard.setData(imgData, forPasteboardType: kUTTypePNG as String)

info.plist :

<dict>
    <key>IsASCIICapable</key>
    <false/>
    <key>PrefersRightToLeft</key>
    <false/>
    <key>PrimaryLanguage</key>
    <string>en-US</string>
    <key>RequestsOpenAccess</key>
    <true/>
</dict>
+2

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


All Articles