【问题标题】:Email a photo taken with imagePickerController通过电子邮件发送使用 imagePickerController 拍摄的照片
【发布时间】:2015-11-03 03:37:26
【问题描述】:

此应用在 viewWillAppear 方法中加载设备的摄像头:

- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];

if (self.imageView.image == nil) {
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
    imagePickerController.delegate = self;
    [self presentViewController:imagePickerController animated:YES completion:nil];
}
else {   }
}

实现了委托方法:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

[self dismissViewControllerAnimated:YES completion:nil];

// Pass the image to email composer after dismissing the camera. Delay allowed for cameraVC to dismiss.
[self performSelector:@selector(composeEmail) withObject:image afterDelay:1.0];
}

- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[[picker parentViewController] dismissViewControllerAnimated:YES completion:nil];
}

当拍摄照片并选择默认的“使用照片”按钮时,我想关闭相机 ViewController 并加载使用此方法的 emailComposer View Controller:

- (void) composeEmail: (UIImage *)image {

NSString *bodyHeader = @"Here are you directions:";
NSString *mailBody = [NSString stringWithFormat:@"%@\n%@", bodyHeader, googleMapsURL];

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

[picker setSubject:@"Google Maps Directions"];
[picker setMessageBody:mailBody isHTML:NO];
[picker setToRecipients:@[@"john.doe@gmail.com"]];
[picker setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

// Create NSData object as PNG image data from camera image
NSData *data = UIImagePNGRepresentation(image);

// Attach image data to the email
// 'DestinationImage.png' is file name that will be attached to the email
[picker addAttachmentData:data mimeType:@"image/png" fileName:@"DestinationImage"];

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

(我在这里遗漏了一些 MessageUI 细节,但我已经对其进行了测试,我知道它可以正常工作)

应将拍摄的图像传递给 emailComposer 并作为电子邮件附加。当我在我的设备上构建它并点击“使用照片”按钮时,会引发错误。错误消息指出“尝试在其视图不在窗口层次结构中的 ViewController 上呈现 MFMailCompseViewController!”我只使用了一个 ViewController,而那个 VC 包含一个 Image View。

谁能帮我关闭相机并加载电子邮件编辑器? 非常感谢!

【问题讨论】:

    标签: ios objective-c uiviewcontroller uiimagepickercontroller mfmailcomposeviewcontroller


    【解决方案1】:

    首先关闭ImagePicker,然后显示您的邮件撰写器将起作用。您将关闭父级,它不起作用[yourpicker dismissViewControllerAnimated:YES completion:nil];

     - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
    UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    [yourpicker dismissViewControllerAnimated:YES completion:nil];
    
    // Pass the image to email composer after dismissing the camera. Delay allowed for cameraVC to dismiss.
    
    [self performSelector:@selector(composeEmail) withObject:image afterDelay:1.0];
     }
    

    【讨论】:

      【解决方案2】:

      要显示视图控制器,请使用以下内容:

      [[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController: picker
                                                                                                       animated:YES
                                                                                                     completion:nil];
      

      您也可以查看此stackoverflow's question 以进一步了解

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-10
        • 2014-04-08
        相关资源
        最近更新 更多