Attach a photo to an email from my iPhone application

我的梦境 提交于 2019-11-30 20:30:44

问题


I have an iPhone application that picks a photos from the photo album built in App. Now I want to add a sharing button with an option of sharing this photo by email , I can attach an existing photo through this code :

    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

[picker setSubject:@""];


// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@""]; 
[picker setToRecipients:toRecipients];


// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:@"project existing photo" ofType:@"jpg"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"photo name"];

// Fill out the email body text
NSString *emailBody = @"";
[picker setMessageBody:emailBody isHTML:NO];

[self presentModalViewController:picker animated:YES];
[picker release];

But what do I need to change in this code to attach the picked photo album into the email body ? Thanks in advance.


回答1:


Use UIImagePickerController to allow the user to pick an image. It will then call this delegate method.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
    NSData* data = UIImageJPEGRepresentation(image, 1.0);
    // Your e-mail code here
}



回答2:


Hi Use UIImagePicker For Select Image From PhotoLibrary of Camera And Use MFMailComposeViewController For send the email.

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

// Dismiss PickerViewController 

[picker dismissModalViewControllerAnimated:NO]; 

// Get Image Fro Attachment

UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData* data = UIImageJPEGRepresentation(image, 1.0);

// Setup Email Settings Like Subject, Message , Attachment


MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
mailPicker.mailComposeDelegate = self;

[mailPicker setSubject:@"Image Attachment Test"];


// Set recipients

NSArray *toRecipients = [NSArray arrayWithObject:@"xyz.gmail.com"]; 
[mailPicker setToRecipients:toRecipients];

// Set body message here
NSString *emailBody = @":)";
[picker setMessageBody:emailBody isHTML:NO];

// Attach Image as Data 
[mailPicker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"photo name"];

[self presentModalViewController:mailPicker animated:YES];

[mailPicker release];


}



回答3:


Assuming you have a UIImage from the image picker (or any other source), you first need to create an NSData object from the image. Use either the UIImageJPEGRepresentation or the UIImageJPEGRepresentation functions. Once you have the NSData object, add it as an attachment like you did in the code you posted.

In most cases, the image will appear in the email after the main message body.



来源:https://stackoverflow.com/questions/12966944/attach-a-photo-to-an-email-from-my-iphone-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!