【问题标题】:Unable to attach image to in-app SMS无法将图像附加到应用内短信
【发布时间】: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


    【解决方案1】:

    改变这一行:-

    composeVC.addAttachmentData(imageData!, typeIdentifier: "image/png", filename:"my image")
    

    到这里:-

     composeVC.addAttachmentData(imageData!, typeIdentifier: "image/png", filename:"myimage.png")
    

    你去!

    【讨论】:

    • 太棒了!有用!知道为什么composeVC.addAttachmentData(imageData!, typeIdentifier: "image/png", filename:"my image") 适用于我的应用内电子邮件而不是应用内短信吗?
    • 你指定了错误的 "UTI (Uniform Type Identifier)" ,检查developer.apple.com/library/prerelease/ios/documentation/…
    • 太棒了! Terima Kasih :)
    • Saya berbesar hati ;)
    • 哈哈哈!太棒了,我的朋友!
    猜你喜欢
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 2011-06-05
    • 1970-01-01
    相关资源
    最近更新 更多