Search code examples
iphoneiosxcodeemail-attachmentsfile-sharing

Attach a photo to an email from my iPhone application


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.


Solution

  • 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
    }