无法将图像附加到应用内短信
Unable to attach image to in-app SMS
我有一个简单的应用程序,可以截取部分屏幕截图,然后通过短信发送生成的图像。
弹出短信对话框中附加到短信的图片 window 不是我通过屏幕截图捕获的图片 - 它是 'blank' 图片。
这是我的代码:
@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)
}
}
我已经为此研究了几个小时,但看不出哪里出了问题。
我有相同的代码将附件添加到应用程序内电子邮件,但控制器有明显差异,而且工作正常。只是无法处理应用内短信。
谢谢!
更改此行:-
composeVC.addAttachmentData(imageData!, typeIdentifier: "image/png", filename:"my image")
对此:-
composeVC.addAttachmentData(imageData!, typeIdentifier: "image/png", filename:"myimage.png")
给你!
我有一个简单的应用程序,可以截取部分屏幕截图,然后通过短信发送生成的图像。
弹出短信对话框中附加到短信的图片 window 不是我通过屏幕截图捕获的图片 - 它是 'blank' 图片。
这是我的代码:
@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)
}
}
我已经为此研究了几个小时,但看不出哪里出了问题。
我有相同的代码将附件添加到应用程序内电子邮件,但控制器有明显差异,而且工作正常。只是无法处理应用内短信。
谢谢!
更改此行:-
composeVC.addAttachmentData(imageData!, typeIdentifier: "image/png", filename:"my image")
对此:-
composeVC.addAttachmentData(imageData!, typeIdentifier: "image/png", filename:"myimage.png")
给你!