【问题标题】:Can't Attach Image to MFMessageComposeViewController无法将图像附加到 MFMessageComposeViewController
【发布时间】:2014-06-03 18:16:17
【问题描述】:

我正在执行以下操作,但是在记录时,它总是返回无法附加图像。这里有什么问题?

- (void)showInvitation {

if (![MFMessageComposeViewController canSendText]) {

    UIAlertView *warningAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Your device doesn't support SMS!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [warningAlert show];
    return;
}

NSString *message = [NSString stringWithFormat:@"Download this game!"];
MFMessageComposeViewController *messageController = [[MFMessageComposeViewController alloc] init];
messageController.messageComposeDelegate = self;
[messageController setBody:message];

if ([MFMessageComposeViewController canSendAttachments]) {
    NSLog(@"Attachments Can Be Sent.");
    NSData *imgData = [NSData dataWithContentsOfFile:@"water"];
    BOOL didAttachImage = [messageController addAttachmentData:imgData typeIdentifier:(NSString *)kUTTypePNG filename:@"image.png"];

    if (didAttachImage) {
        NSLog(@"Image Attached.");

    } else {
        NSLog(@"Image Could Not Be Attached.");
    }
}

[self presentViewController:messageController animated:YES completion:nil];
}

【问题讨论】:

  • 什么账号?也许您的 ISP 阻止了彩信?尝试使用 iMessage。
  • 它确实说当我将其登录到控制台时可以发送附件。如何尝试使用 imessage?
  • 实际上是的,我正在使用 iMessage。
  • 为什么不将附件添加为文件?您已经在磁盘上拥有它,创建一个 NSURL 并使用其他方法。
  • 哇!这实际上最终起作用了!谢谢!!!

标签: objective-c ios7 uiimage mfmessagecomposeview messageui


【解决方案1】:

如 cmets 中所述,请使用 addAttachmentURL:withAlternateFilename:。我的猜测是,您提供的 NSData 对象不适合 kUTTypePNG 类型并且添加附件失败。

【讨论】:

    【解决方案2】:

    请尝试此代码。它对我来说很好。

     if (MFMessageComposeViewController.canSendText()) {
    
    
                let controller = MFMessageComposeViewController()
    
    controller.body = "Solution of broken image in composer while sending through MFMessageComposserViewController "
    
    
                controller.messageComposeDelegate = self
    
                if image.imageAsset != nil {
    
                    let imageData = UIImageJPEGRepresentation(self.fixOrientation(img: image), 1)  //! as NSData
                    controller.addAttachmentData(imageData! , typeIdentifier: "image/.jpeg", filename: "image.jpeg")
    
                }
    
                self.present(controller, animated: true, completion: {
                    completion(true)
                })
    
    
            }
            }
    

    【讨论】:

    • 问题已经得到解答和解决。 OP 不太可能使用它,因为这个问题已经存在 3 年了。另外请注意格式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 2011-06-05
    • 2014-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多