通过照片库发送图片

时间:2013-06-05 09:03:27

标签: ios objective-c uiimagepickercontroller mfmailcomposeviewcontroller

我想撰写一封电子邮件,其中包含从相机或照片库中拍摄的照片。但是我无法打开Mail作曲家选择器。

这是我的代码:

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
    [self dismissModalViewControllerAnimated:YES];
    UIImage* image_type=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
    dataImage = UIImagePNGRepresentation(image_type);

    if([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
        mailCont.delegate=self;
        mailCont.mailComposeDelegate = self;        // Required to invoke mailComposeController when send
        [mailCont setSubject:@""];
        [mailCont setToRecipients:[NSArray arrayWithObject:@""]];
        [mailCont addAttachmentData:dataImage mimeType:@"image/png" fileName:@"sender_image.png"];
        [mailCont setMessageBody:@"" isHTML:NO];

        [self presentModalViewController:mailCont animated:YES];
    }
}

现在邮件选择器没有打开。警告是:

Warning: Attempt to present <MFMailComposeViewController: 0xa26b070> on <UINavigationController: 0xa22e6d0> while a presentation is in progress!

我该如何处理。

1 个答案:

答案 0 :(得分:2)

您的问题是您首先解雇UIImagePicker,然后立即尝试将另一个视图显示为模态视图。这必须在解雇结束后完成。试试这个:

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
    [self dismissViewControllerAnimated:YES
                             completion:^{
                                 UIImage* image_type=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
                                 dataImage = UIImagePNGRepresentation(image_type);

                                 if([MFMailComposeViewController canSendMail])
                                 {
                                     MFMailComposeViewController *mailCont = [[MFMailComposeViewController alloc] init];
                                     mailCont.delegate=self;
                                     mailCont.mailComposeDelegate = self;        // Required to invoke mailComposeController when send
                                     [mailCont setSubject:@""];
                                     [mailCont setToRecipients:[NSArray arrayWithObject:@""]];
                                     [mailCont addAttachmentData:dataImage mimeType:@"image/png" fileName:@"sender_image.png"];
                                     [mailCont setMessageBody:@"" isHTML:NO];

                                     [self presentModalViewController:mailCont animated:YES];
                                 }
                             }];
}

希望它有所帮助!