This question already has an answer here:
Attaching an image to an email?
(1 answer)
Closed 8 years ago.
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
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.
Related
I use the below code snippet to open the camera, click a photo and use the photo (when the two options, Retake and Use Photo, appear).
- (IBAction)openCamera
{
#autoreleasepool {
[imageSpinner stopAnimating];
[imageSpinner removeFromSuperview];
[cameralabel setText:#""];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType =
UIImagePickerControllerSourceTypeCamera;
imagePicker.allowsEditing = NO;
[self presentViewController:imagePicker animated:NO completion:nil];
imagePicker=nil;
}
}
}
-(void)setImageSpinner
{
#autoreleasepool {
imageSpinner = nil;
imageSpinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
imageSpinner.center = self.view.center;
[imageSpinner startAnimating];
[self.view addSubview:imageSpinner];
[self.view bringSubviewToFront:imageSpinner];
}
}
- (void)viewDidLoad
{
[self openCamera];
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setToolbarHidden:YES];
self.navigationItem.hidesBackButton = NO;
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *savedImagePath;
#autoreleasepool {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"Image 1.jpeg"];
paths=nil;
documentsDirectory=nil;
[picker dismissViewControllerAnimated:NO completion:NULL];
[cameralabel setText:#"Please wait"];
[self setImageSpinner];
}
#autoreleasepool {
ManifestDocumentViewController *manifestVC = [self.storyboard instantiateViewControllerWithIdentifier:#"manifestImage"];
manifestVC.getTempFilePath = savedImagePath;
if ([screenName isEqualToString:#"Retake"])
{
manifestVC.screenName = #"Retake";
manifestVC.retakeFileID = retakeFileID;
manifestVC.retakeFilePath = retakeFilePath;
NSData *data = [NSData dataWithContentsOfFile:savedImagePath];
[data writeToFile:retakeFilePath atomically:YES];
data=nil;
}
else
{
manifestVC.screenName = #"Camera";
UIImage *image = info[UIImagePickerControllerOriginalImage];
NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];
[imageData writeToFile:savedImagePath atomically:NO];
image=nil;
imageData=nil;
}
savedImagePath=nil;
[self.navigationController pushViewController:manifestVC animated:YES];
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:NULL];
picker.delegate=nil;
picker=nil;
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
}
During a debugging session on iPhone 4s, the memory spikes up as shown by the memory usage under the Debug navigator. Attached along side is a screenshot of the Memory Usage comparison. The raise in 'Other Processes' is a matter of concern.
Received memory warning also appears among the displayed logs. After running the code a number of times the app crashes (The message 'Lost connection to iPhone'). This corresponds to a spike in the memory and the 'Free' memory being consumed completely.
Can the above code be optimised as far as possible to ensure that the memory usage gets reduced as much as possible.
I have developed an app that allows the user to choose a video from the photo gallery and send it as an attachment in an email. I am able to choose a video from the gallery and proceed with sending the email but the video does not get attached with the email. There are no errors in the console.
ViewController.h:
#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <MessageUI/MessageUI.h>
#interface ViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate,MFMailComposeViewControllerDelegate>
#property (strong, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)choose:(id)sender;
#end
ViewController.m:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIAlertController *myAlertController = [UIAlertController alertControllerWithTitle:#"MyTitle"
message: #"MyMessage"
preferredStyle:UIAlertControllerStyleAlert ];
[self presentViewController:myAlertController animated:YES completion:nil];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)choose:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[self performSelector:#selector(email:) withObject:chosenImage afterDelay:0.5];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)email:(UIImage *)choosenImage{
NSString *iOSVersion = [[UIDevice currentDevice] systemVersion];
NSString *model = [[UIDevice currentDevice] model];
NSString *version = #"1.0";
NSString *build = #"100";
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
[mailComposer setToRecipients:[NSArray arrayWithObjects: #"support#myappworks.com",nil]];
[mailComposer setSubject:[NSString stringWithFormat: #"MailMe V%# (build %#) Support",version,build]];
NSString *supportText = [NSString stringWithFormat:#"Device: %#\niOS Version:%#\n\n",model,iOSVersion];
supportText = [supportText stringByAppendingString: #"Please describe your problem or question."];
[mailComposer setMessageBody:supportText isHTML:NO];
NSData *data = UIImagePNGRepresentation(choosenImage);
[mailComposer addAttachmentData:data mimeType:#"image/png" fileName:#""];
[self presentViewController:mailComposer animated:YES completion:nil];
}
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
Any suggestion/help would be much appreciated. Thank you.
You mentioned that you're trying to attach a video, and you've configured your UIImagePickerController to limit the mediaTypes to only videos. The problem then is that you're asking for the "edited image" in the "didFinishPickingMediaWithInfo" method:
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
The user did not pick an image - they picked a video. You need to use this instead:
NSURL *chosenVideoUrl = info[UIImagePickerControllerMediaURL];
NSData *videoData = [NSData dataWithContentsOfURL:chosenVideoUrl];
You can then pass the videoData to your email method and attach to the email. Be sure to update the mimeType from "image/png" to "video/mp4", as well.
If u need to attach both video and image you have write to code for both,but you written only for attaching an image.You can try the code below for getting both
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
if ([[info objectForKey:UIImagePickerControllerMediaType] isEqual:(NSString *)kUTTypeMovie]) {
NSString *videoURL = info[UIImagePickerControllerMediaURL];
[self emailImage:nil orVideo:videoURL];
}else {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[self emailImage:chosenImage orVideo:nil];
}
[picker dismissViewControllerAnimated:YES completion:NULL];
}
UIImagePickerControllerMediaURL will return file url unlike UIImagePickerControllerEditedImage ,so can use NSData method dataWithContentsOfFile as bellow.
if (choosenImage) {
NSData *data = UIImagePNGRepresentation(choosenImage);
NSString *filename = [NSString stringWithFormat:#"Image_%#.png",TimeStamp];
[mailComposer addAttachmentData:data mimeType:#"image/png" fileName:filename];
[self presentViewController:mailComposer animated:YES completion:nil];
}else {
NSData *data = [NSData dataWithContentsOfFile:videoFile];
NSString *filename = [NSString stringWithFormat:#"Video_%#.mp4",TimeStamp];
[mailComposer addAttachmentData:data mimeType:#"video/mp4" fileName:filename];
[self presentViewController:mailComposer animated:YES completion:nil];
}
it will be good if you give a filename for the attachment it will be help full after it downloading.if you wish you can use a TimeStamp for that.
#define TimeStamp [NSString stringWithFormat:#"%f",[[NSDate date] timeIntervalSince1970] * 1000]
I'm creating a simple game app.
When the user click on the first button and the int click is 0, the he can click on the second button, and so on.
If the user firstly click on the second button and then on the first, his lifes will decreasing.
I'm having problems when the user reaches 0 lifes and the View returns on the first page.
Then when I click on the button to play again, I have 0 lifes and don't 3!
Here is my code:
#implementation livello1
int click=0;
int lifes=3;
-(void)updateLifes:(int)lifes{
if (lifes==1){
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
[standardDefaults setObject:#"1" forKey:#"UserLifes"];
[standardDefaults synchronize];
[self showLifes];
livello1 *livello1view = [[livello1 alloc] initWithNibName:nil bundle:nil];
livello1view.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:livello1view animated:YES completion:nil];
}
else if (lifes==2){
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
[standardDefaults setObject:#"2" forKey:#"UserLifes"];
[standardDefaults synchronize];
[self showLifes];
livello1 *livello1view = [[livello1 alloc] initWithNibName:nil bundle:nil];
livello1view.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:livello1view animated:YES completion:nil];
}
else if (lifes==3){
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
[standardDefaults setObject:#"3" forKey:#"UserLifes"];
[standardDefaults synchronize];
[self showLifes];
livello1 *livello1view = [[livello1 alloc] initWithNibName:nil bundle:nil];
livello1view.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:livello1view animated:YES completion:nil];
}
else if (lifes==0){
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
lifes=3;
[self showLifes];
[standardDefaults setObject:#"3" forKey:#"UserLifes"];
[standardDefaults synchronize];
gioca *giocaview = [[gioca alloc] initWithNibName:nil bundle:nil];
giocaview.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:giocaview animated:YES completion:nil];
}
}
-(void)showLifes{
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
if ([[standardDefaults stringForKey:#"Userlifes"] isEqualToString:#"1"]) {
lifes=1;}
if ([[standardDefaults stringForKey:#"Userlifes"] isEqualToString:#"2"]) {
lifes=2;}
if ([[standardDefaults stringForKey:#"Userlifes"] isEqualToString:#"3"]) {
lifes=3;}
lifesLabel.text = [NSString stringWithFormat:#"%i",lifes];
}
- (IBAction)press1:(UIButton *)button1{
if ((button1.selected=YES && click==0)){
click=1;
}
else{
lifes=lifes-1;
click=0;
[self updateLifes:(lifes)];
}
}
- (IBAction)press2:(UIButton *)button2{
if ((button2.selected =YES) && click==1){
click=2;
}
else{
lifes=lifes-1;
click=0;
[self updateLifes:(lifes)];
}
}
- (void)viewDidLoad{
[super viewDidload];
[self showLifes];
}
Create another button for "Play Again" and link it to -(IBAction)buttonPressed:(UIButton *)playAgain and in that function reset the value of life to 3. And you don't need to pass lifes as it is a global variable (though that is not a very good practice). Though I am not really sure about your logic/ implementation for the click, you can use the following (slightly optimised) code :
#implementation livello1
int click=0;
int lifes=3;
-(void)updateLifes{
if (lifes!=0) {
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
[standardDefaults setInteger:lifes forKey:#"UserLifes"];
[standardDefaults synchronize];
[self showLifes];
livello1 *livello1view = [[livello1 alloc] initWithNibName:nil bundle:nil];
livello1view.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:livello1view animated:YES completion:nil];
}
else {
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
lifes=3;
[self showLifes];
[standardDefaults setObject:#"3" forKey:#"UserLifes"];
[standardDefaults synchronize];
gioca *giocaview = [[gioca alloc] initWithNibName:nil bundle:nil];
giocaview.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:giocaview animated:YES completion:nil];
}
}
-(void)showLifes{
NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
lifesLabel.text = [NSString stringWithFormat:#"%i",[standardDefaults integerForKey:#"Userlifes"]];
}
- (IBAction)press1:(UIButton *)button1{
if ((button1.selected=YES && click==0)){
click=1;
}
else{
lifes=lifes-1;
click=0;
[self updateLifes];
}
}
- (IBAction)press2:(UIButton *)button2{
if ((button2.selected =YES) && click==1){
click=2;
}
else{
lifes=lifes-1;
click=0;
[self updateLifes];
}
}
- (IBAction)buttonPressed:(UIButton *)playAgain{ //the new function
lifes = 3;
click = 0;
[self updateLifes];
}
- (void)viewDidLoad{
[super viewDidload];
[self showLifes];
}
on playAgain Button click, setLife counter to 3.
This question already has answers here:
Attach a photo to an email from my iPhone application
(3 answers)
Closed 9 years ago.
I am having a problem where I'm attaching text and a image to the mail composer my textfields attach fine however when i try to attach an image selected from my camera roll i get a question mark? is it because i have not taken a picture from a ios device, and i am just downloading images from google to test it. Also the application works as follows:
user takes picture and sees it in the UIImageView and then can type his contact details and name and send, or can choose a picture from their camera roll.
.h
#interface xyzViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationBarDelegate> {
IBOutlet UILabel *label;
IBOutlet UITextField *field;
}
#property (strong, nonatomic) IBOutlet UIImageView *imageView;
#property (strong, nonatomic) IBOutlet UIButton *TakePhoto;
#property (strong, nonatomic) IBOutlet UIButton *SelectPhoto;
- (IBAction)savedata:(id)sender;
- (IBAction)loaddata:(id)sender;
- (IBAction)dismiss:(id)sender;
- (IBAction)dismiss1:(id)sender;
#property (weak, nonatomic) IBOutlet UITextField *mytextview;
#property (weak, nonatomic) IBOutlet UITextField *mytextview1;
#property (weak, nonatomic) IBOutlet UIImageView *image;
#end
.m file
#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
to take photo use
- (IBAction)takePhotoCam:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
[self.navigationController presentViewController:imagePicker animated:YES completion:nil];
}
To choose from gallery use
- (IBAction)choosePhotoFromLibrary:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
[self.navigationController presentViewController:imagePicker animated:YES completion:nil];
}
Now use delegate to read that photo and attach in email...
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
NSData *pngData = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
filePath = [documentsPath stringByAppendingPathComponent:#"myCameraImage.png"];
NSLog(#"saving data at === %#", filePath);
[pngData writeToFile:filePath atomically:YES]; //Write the file
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
Now in Documents folder, you have image called myCameraImage.png
Now let's say you have button called Send on email. On clicking create IBAction and attach myCameraImage.png to email.
See here, how to attach image to email.
I am developing an app for iOS 7 and used MFMailComposerViewController.
I have tried everything but dismissViewController:withAnimated is not working.
sometimes class automatically call delegate by itself when it first displays viewController using method presentViewCOntroller:withAnimated:completion.
My app is navigation based that's why I think issue is just related with UINavigationController as well.
-(void)sendMail{
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:#"Hello from California!"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:#"first#example.com"];
NSArray *ccRecipients = [NSArray arrayWithObjects:#"second#example.com", #"third#example.com", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:#"fourth#example.com"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
// Fill out the email body text
NSMutableString *emailBody =[NSMutableString stringWithString: #"<table border=1 align=\"center\"><tr><th>EventDate</th><th>EventDay</th><th>EventTime</th><th>Speaker</th><th>topic</th></tr>"];
for (int i=0; i<5; i++) {
NSString *eventDate=[NSString stringWithFormat:#"<tr><td>%#</td>",#"12/11"];
NSString *eventDay=[NSString stringWithFormat:#"<td>%#</td>",#"Sunday"];
NSString *eventTime=[NSString stringWithFormat:#"<td>%#</td>",#"12:10 pm"];
NSString *eventSpeaker=[NSString stringWithFormat:#"<td>%#</td>",#"RajVeer"];
NSString *eventTopic=[NSString stringWithFormat:#"<td>%#</td>",#"nano-technology"];
NSString *dataString=[NSString stringWithFormat:#"%#%#%#%#%#</tr>",eventDate,eventDay,eventTime,eventSpeaker,eventTopic];
[emailBody appendString:dataString];
}
NSString *lastTable=#"</table>";
[emailBody appendString:lastTable];
NSLog(#"%#",emailBody);
[picker setMessageBody:emailBody isHTML:YES];
[self presentViewController:picker animated:YES completion:NULL];
}
This should do the trick:
#pragma mark MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[controller dismissViewControllerAnimated:YES completion:nil];
}
Use this code to present MFMailComposeViewController
[self presentViewController:mailComposerObject animated:YES completion:NULL];
For dismiss MFMailComposeViewController
#pragma mark - MFMailComposeViewControllerDelegate
-(void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult: (MFMailComposeResult)result error:(NSError*)error {
[self dismissViewControllerAnimated:YES completion:NULL];
}
from iOS 6.0 [self presentModalViewController:<#(UIViewController *)#> animated:<#(BOOL)#>] is deprecated.