我有一个简单的应用程序,它采用部分截图,然后短信生成图像。
弹出的SMS对话窗口中附加到SMS的图像不是我通过屏幕截图捕获的图像 - 它是一个“空白”图像。
这是我的代码:
@IBAction func smsScreenShot(sender: AnyObject) {
// Declare the snapshot boundaries
let top: CGFloat = 100
let bottom: CGFloat = 60
// The size of the cropped image
let size = CGSize(width: view.frame.size.width, height: view.frame.size.height - top - bottom)
// Start the context
UIGraphicsBeginImageContext(size)
// use context in a couple of places
let context = UIGraphicsGetCurrentContext()!
// Transform the context so that anything drawn into it is displaced "top" pixels up
CGContextTranslateCTM(context, 0, -top)
// Draw the view into the context (this is the snapshot)
view.layer.renderInContext(context)
let snapshot = UIGraphicsGetImageFromCurrentImageContext()
// End the context (this is required to not leak resources)
UIGraphicsEndImageContext()
// Composing the SMS
if !MFMessageComposeViewController.canSendText() {
print("SMS services are not available")
}
if (MFMessageComposeViewController.canSendText()) {
let composeVC = MFMessageComposeViewController()
composeVC.messageComposeDelegate = self
composeVC.recipients = []
composeVC.body = "Have a look at this image!!";
// Attaching the image to the SMS.
let image = snapshot
let imageData = UIImagePNGRepresentation(image)
composeVC.addAttachmentData(imageData!, typeIdentifier: "image/png", filename:"my image")
self.presentViewController(composeVC, animated: true, completion: nil)
}
}
我已经研究了几个小时了,不知道我哪里出错了。
我有相同的代码将附件添加到应用内电子邮件中,控制器中存在明显差异,并且工作正常。只是不处理应用程序内的短信。
谢谢!
答案 0 :(得分:0)
更改此行: -
<AssemblyVersion>
到此: -
composeVC.addAttachmentData(imageData!, typeIdentifier: "image/png", filename:"my image")
你去吧!