Attach a photo to an email from my iPhone application

后端 未结 3 987
星月不相逢
星月不相逢 2021-01-07 10:22

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

相关标签:
3条回答
  • 2021-01-07 10:57

    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
    }
    
    0 讨论(0)
  • 2021-01-07 11:02

    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.

    0 讨论(0)
  • 2021-01-07 11:03

    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];
    
    
    }
    
    0 讨论(0)
提交回复
热议问题