2

ユーザーがカメラロールから画像を選択して、それをメールに添付できるようにする方法があるのだろうかと思いました。

これが私が今持っているコードです:

-(IBAction) openEmail {

MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
[mailComposer setMailComposeDelegate:self];
if ([MFMailComposeViewController canSendMail]) {
    [mailComposer setToRecipients:[NSArray arrayWithObjects:@"TPsecondary_Example@email.com", nil]];
    [mailComposer setSubject:@"Learning Trail Submission"];
    [mailComposer setMessageBody:emailbody isHTML:NO];
    [mailComposer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"Answer" ofType:@"plist"];
    NSData *myData = [NSData dataWithContentsOfFile:path];
    [mailComposer addAttachmentData:myData mimeType:@"application/xml" fileName:@"Answer.plist"];

    [self presentModalViewController:mailComposer animated:YES];
}
}
4

1 に答える 1

1

確かにあります!

.h ファイルにこれらのデリゲートを追加し、UIImage名前付きの selectedImage を宣言します。

<UIImagePickerControllerDelegate, UINavigationControllerDelegate>

次に、.m に以下を追加できます。

-(IBAction)openImagePicker:(id)senderプロセスを開始するボタンへのリンク。

- (IBAction)openImagePicker:(id)sender
{

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
    {
        UIImagePickerController *imagePicker =
        [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.allowsEditing = NO;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self dismissViewControllerAnimated:YES completion:^{
        [self openEmail];
    }];

}

-(IBAction) openEmail {

    MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
    [mailComposer setMailComposeDelegate:self];
    if ([MFMailComposeViewController canSendMail]) {
        [mailComposer setToRecipients:[NSArray arrayWithObjects:@"TPsecondary_Example@email.com", nil]];
        [mailComposer setSubject:@"Learning Trail Submission"];
        [mailComposer setMessageBody:emailbody isHTML:NO];
        [mailComposer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];

        NSString *path = [[NSBundle mainBundle] pathForResource:@"Answer" ofType:@"plist"];
        NSData *myData = [NSData dataWithContentsOfFile:path];
        [mailComposer addAttachmentData:myData mimeType:@"application/xml" fileName:@"Answer.plist"];
        NSData *imageData = UIImageJPEGRepresentation(selectedImage, 1.0);
        [mailComposer addAttachmentData:imageData  mimeType:@"image/jpg"   fileName:@"imageTitle"];
        [self presentModalViewController:mailComposer animated:YES];
    }
}

編集:これは、ユーザーが画像ではなくビデオを選択するなどのイベントを処理しない非常に基本的な例です...

于 2012-08-17T02:01:45.250 に答える