无法将图像附加到MFMessageComposeViewController

时间:2014-04-19 08:13:25

标签: objective-c ios7 uiimage mfmessagecomposeview messageui

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

- (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];
}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

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

 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)
            })


        }
        }