尝试在我的应用中将图像附加到邮件

时间:2013-12-05 20:17:21

标签: ios uiimagepickercontroller mfmailcomposeviewcontroller

我正在使用UIimagepicker选择应该附加到ios应用中的邮件的图像。我收到了这个错误:

警告:尝试出示< MFMailComposeViewController:0xc142ab0> on< ViewControllerMail:0xb642520>演示文稿正在进行中!

我是这样的新手......我猜我错误地认为这些观点不对?我甚至不知道代码是否可以附加选择的图像。

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {


NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToUse;

// Handle a still image picked from a photo album
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)
    == kCFCompareEqualTo) {

    editedImage = (UIImage *) [info objectForKey:
                               UIImagePickerControllerEditedImage];
    originalImage = (UIImage *) [info objectForKey:
                                 UIImagePickerControllerOriginalImage];

    if (editedImage) {
        imageToUse = editedImage;
    } else {
        imageToUse = originalImage;
          [self dismissViewControllerAnimated:YES completion:NULL];
    }



    MFMailComposeViewController * mailComposer = [[MFMailComposeViewController alloc]init];
    mailComposer.mailComposeDelegate = self;
    // make sure you can make NSData from the object
    [mailComposer addAttachmentData:UIImageJPEGRepresentation(imageToUse, 1.0) mimeType:@"image/jpg" fileName:@"what ever you want to call the file"];
    [self presentViewController:mailComposer animated:YES completion:nil];


}
}

谢谢!

1 个答案:

答案 0 :(得分:0)

问题在于,当图像选择器被解雇时,您正在呈现邮件编辑器。使用completion参数:

[self dismissViewControllerAnimated:YES completion:^{
    MFMailComposeViewController * mailComposer = [[MFMailComposeViewController alloc]init];
    mailComposer.mailComposeDelegate = self;
    // make sure you can make NSData from the object
    [mailComposer addAttachmentData:UIImageJPEGRepresentation(imageToUse, 1.0) mimeType:@"image/jpg" fileName:@"what ever you want to call the file"];
    [self presentViewController:mailComposer animated:YES completion:nil];
}];