0

So i have created so far 2 text fields which, the text fields are connected to the mail composer and whatever the user writes in them is what comes up in the mail composer however i do not know how to do the same for the UIImageView, i want the user to choose a picture or take a picture and that be automatically added to the mail composer as well.

@interface xyzViewController ()

@end

@implementation xyzViewController


- (IBAction)savedata:(id)sender; {
NSString *savestring = _mytextview.text;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:savestring forKey:@"savedstring"];
[defaults synchronize];

NSString *savestring1 = _mytextview1.text;
NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
[defaults1 setObject:savestring1 forKey:@"savedstring1"];
[defaults synchronize];

}

- (IBAction)loaddata:(id)sender; {

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *loadstring = [defaults objectForKey:@"savedstring"];
[_mytextview setText:loadstring];
[label setText:loadstring];

NSUserDefaults *defaults1 = [NSUserDefaults standardUserDefaults];
NSString *loadstring1 = [defaults objectForKey:@"savedstring1"];
[_mytextview1 setText:loadstring1];

}

- (IBAction)dismiss:(id)sender {
[sender resignFirstResponder];
}
- (IBAction)dismiss1:(id)sender {
[sender resignFirstResponder];
}


 - (IBAction)takePhoto:(UIButton *)sender {

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;

[self presentViewController:picker animated:YES completion:NULL];

}

- (IBAction)selectPhoto:(UIButton *)sender {

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

[self presentViewController:picker animated:YES completion:NULL];


}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;

[picker dismissViewControllerAnimated:YES completion:NULL];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

[picker dismissViewControllerAnimated:YES completion:NULL];

}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

- (IBAction)sendButton:(id)sender {
MFMailComposeViewController *mailContoller = [[MFMailComposeViewController alloc]init];
[mailContoller setMailComposeDelegate:self];
NSString *email = @"avip606@gmail.com";
NSString *email1 = @"avi_sp@hotmail.co.uk";
NSArray *emailArray = [[NSArray alloc]initWithObjects:email, email1, nil];
NSString *message = [@[_mytextview.text, _mytextview1.text] componentsJoinedByString: @"\n"];
[mailContoller setMessageBody:message isHTML:NO];
NSData *data = UIImagePNGRepresentation(_image);
[mailContoller addAttachmentData:data
                        mimeType:@"image/png"
                        fileName:@"image.png"];
[mailContoller setToRecipients:emailArray];
[mailContoller setSubject:@"IT WORKS!"];
[self presentViewController:mailContoller animated:YES completion:nil];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[[self mytextview] resignFirstResponder];
}

-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:nil];
}


@end
avi
  • 107
  • 1
  • 1
  • 8

1 Answers1

3

Of course you can attach an image to a message, if you're using the MFMailComposeViewController class.

You just need to figure out how to use it's API of "addAttachmentData: mimeType: fileName:".

This related question might provide the answer you're looking for.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • is that from in the app, i want to let the user choose which image they attach not pre-define it, sorry if this all sounds stupid i am new to this. – avi Sep 28 '13 at 15:57
  • You'd have your user select the image s/he wants to mail *before* you bring up the MFMailComposeViewController. The easiest way to select an image is via UIImagePickerController ([here's a tutorial that talks about it](http://www.appcoda.com/ios-programming-camera-iphone-app/), and once you have the image you want to send, bring up the mail compose view ([and here's another tutorial that talks about that](http://iphonedevsdk.com/forum/tutorial-discussion/43633-quick-tutorial-on-how-to-add-mfmailcomposeviewcontroller.html)). Hopefully you're on the right track now! – Michael Dautermann Sep 28 '13 at 16:01
  • i did that and now just stuck on how to add the photo to the mail composer. – avi Sep 28 '13 at 18:39
  • You create the mail composer and then call "`addAttachmentData:`" to it with the raw data of your selected image. – Michael Dautermann Sep 28 '13 at 18:40
  • hi, i am sorry to bother you so much but is there any chance you can show me an example – avi Sep 28 '13 at 22:28
  • where are you stuck? Did the other question that vikingosegundo pointed to in his comment help? – Michael Dautermann Sep 28 '13 at 22:31
  • a little yes, the problem i am having is i am still new to this thats why i pasted my code above, i do not know were or how i would insert the code. – avi Sep 28 '13 at 22:43
  • I don't see "`addAttachmentData: mimeType: fileName:`" anywhere in your "`sendButton`" method up there. I'm parked on [chat.stackoverflow.com](http://chat.stackoverflow.com/rooms/682/iphone-ipad) right now. Come and say hello if you are still stuck. – Michael Dautermann Sep 28 '13 at 22:47
  • i cant join because i do not have 20 reputation points but i have updated the code and if you could take a look i would greatly appreciated. – avi Sep 28 '13 at 22:59
  • Looks like you just need your mime type to be corrected. – Michael Dautermann Sep 28 '13 at 23:01
  • i changed the mime type to image1/png but still nothing when i click send to open the mail composer theres just a small question mark in the box – avi Sep 28 '13 at 23:37
  • the mime type is `@"image/png"` with no "1" in it. – Michael Dautermann Sep 28 '13 at 23:39
  • well what i am doing is opening a selected photo which i downloaded from google images, would this work if i used an actual ios device and not the emulator? – avi Sep 28 '13 at 23:40
  • in the .h file i have created a IBoulet for image would this be the reason it is not working? – avi Sep 28 '13 at 23:55