我正在构建我的第一个自定义键盘。我使用的是Swift 2和Xcode 7.我把它作为我的键盘
(我在我的iPhone上运行它)当我点击外星人的小脸时,我想要有
带有该图片的小表情符号或
将图像(如果可能)插入用户键入的位置。我试过这段代码
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)
但我没有成功。当我print(data)
时,我会收到nil
,然后在下一行收到EXC_BAD_ACCESS
。我如何实现我的两个目标中的任何一个?谢谢你的帮助。
答案 0 :(得分:4)
对于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>