通过电子邮件发送使用 imagePickerController 拍摄的照片
Email a photo taken with 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];
}
拍摄照片并选择默认 "Use Photo" 按钮后,我想关闭相机 ViewController 并加载使用此方法的 emailComposer 视图控制器:
- (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 并作为电子邮件附件。当我在我的设备上构建它并点击 "Use Photo" 按钮时,会抛出一个错误。错误消息指出 "Attempt to present MFMailCompseViewController on ViewController whose view is not in the window hierarchy!" 我只使用一个 ViewController 并且 VC 包含一个图像视图。
谁能帮我关闭摄像头并加载电子邮件编辑器?
非常感谢!
要显示视图控制器,请使用以下内容:
[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController: picker
animated:YES
completion:nil];
您也可以查看此 Whosebug's question 以进一步了解
首先关闭 ImagePicker
然后显示你的邮件编辑器将工作 then.U r 关闭父 thta 的 y 它不工作 [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];
}
此应用程序在 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];
}
拍摄照片并选择默认 "Use Photo" 按钮后,我想关闭相机 ViewController 并加载使用此方法的 emailComposer 视图控制器:
- (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 并作为电子邮件附件。当我在我的设备上构建它并点击 "Use Photo" 按钮时,会抛出一个错误。错误消息指出 "Attempt to present MFMailCompseViewController on ViewController whose view is not in the window hierarchy!" 我只使用一个 ViewController 并且 VC 包含一个图像视图。
谁能帮我关闭摄像头并加载电子邮件编辑器? 非常感谢!
要显示视图控制器,请使用以下内容:
[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController: picker
animated:YES
completion:nil];
您也可以查看此 Whosebug's question 以进一步了解
首先关闭 ImagePicker
然后显示你的邮件编辑器将工作 then.U r 关闭父 thta 的 y 它不工作 [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];
}