通过电子邮件发送使用imagePickerController拍摄的照片



此应用程序以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包含一个图像视图。

有人能帮我关掉相机并加载电子邮件编辑器吗?非常感谢!

首先解除ImagePicker,然后呈现您的邮件编辑器,然后即可工作。U r驳回其不工作的父级[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];
 }

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

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

您还可以研究这个stackoverflow的问题,以进一步了解

相关内容

  • 没有找到相关文章

最新更新