【发布时间】:2016-02-01 08:06:15
【问题描述】:
我有一个简单的应用程序,它会截取部分屏幕截图,然后通过短信发送生成的图像。
在弹出的 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)
}
}
我已经研究了几个小时,看不出哪里出错了。
我有相同的代码来将附件添加到应用内电子邮件中,控制器中存在明显差异,并且工作正常。只是不适用于应用内短信。
谢谢!
【问题讨论】:
标签: ios image switch-statement sms attachment