Reputation: 93
I was wondering if there is a way to allow the user to select an image from the camera roll, and then attach it to an email?
Here is the code I have now:
-(IBAction) openEmail {
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
[mailComposer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[mailComposer setToRecipients:[NSArray arrayWithObjects:@"[email protected]", nil]];
[mailComposer setSubject:@"Learning Trail Submission"];
[mailComposer setMessageBody:emailbody isHTML:NO];
[mailComposer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Answer" ofType:@"plist"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[mailComposer addAttachmentData:myData mimeType:@"application/xml" fileName:@"Answer.plist"];
[self presentModalViewController:mailComposer animated:YES];
}
}
Upvotes: 2
Views: 784
Reputation: 130183
There sure is!
In your .h file add these delegates and declare a UIImage
named selectedImage.
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
Then in your .m you can add the following.
Link -(IBAction)openImagePicker:(id)sender
to the button that you want to start the process.
- (IBAction)openImagePicker:(id)sender
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = NO;
[self presentViewController:imagePicker animated:YES completion:nil];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:YES completion:^{
[self openEmail];
}];
}
-(IBAction) openEmail {
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
[mailComposer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
[mailComposer setToRecipients:[NSArray arrayWithObjects:@"[email protected]", nil]];
[mailComposer setSubject:@"Learning Trail Submission"];
[mailComposer setMessageBody:emailbody isHTML:NO];
[mailComposer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Answer" ofType:@"plist"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[mailComposer addAttachmentData:myData mimeType:@"application/xml" fileName:@"Answer.plist"];
NSData *imageData = UIImageJPEGRepresentation(selectedImage, 1.0);
[mailComposer addAttachmentData:imageData mimeType:@"image/jpg" fileName:@"imageTitle"];
[self presentModalViewController:mailComposer animated:YES];
}
}
EDIT: Mind you this is a very basic example that doesn't handle events such as the user selecting a video instead of an image...
Upvotes: 1