简体   繁体   中英

iPhone SDK record video then email it

I was wondering if someone could show me how to record video and add it as attachment to mail composer in IOS. The more specific the better. Thanks.

Check this thread for capturing video programatically Basic description on how to record video in iOs 4

Check this tutorial about mail composer for iOS http://www.iostipsandtricks.com/using-apples-mail-composer/

Mail composer only supports images as direct attachments and other formats are needed to be added as nsdata

Send a file as attachment in objective c

PS : If you ask questions without googling people will probably down-vote it.

.h file

#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <MediaPlayer/MediaPlayer.h>

@interface ViewController : UIViewController <MFMailComposeViewControllerDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
    UIImagePickerController *imagePicker;
    NSURL *videoURL;
}

-(IBAction)submitVideo;

.m file

- (void)viewDidLoad
{
    [super viewDidLoad];

    //set image picker
    imagePicker = [[UIImagePickerController alloc]init];
    NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
    imagePicker.mediaTypes = [mediaTypes filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(SELF contains %@)", @"movie"]];
    imagePicker.delegate = self;
    imagePicker.videoMaximumDuration = 60.0;
    imagePicker.allowsEditing = YES;
    [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}

-(IBAction)submitVideo
{
    [self presentViewController:imagePicker animated:YES completion:nil];
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
    NSLog(@"movie captured %@", info);

    [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(sendMail) userInfo:nil repeats:NO];
    [self dismissViewControllerAnimated:YES completion:nil];
}


-(void)sendMail
{
    if ([MFMailComposeViewController canSendMail]) 
    {        
        MFMailComposeViewController *mail = [[MFMailComposeViewController alloc] init];
        mail.mailComposeDelegate = self;
        [mail addAttachmentData:[NSData dataWithContentsOfURL:videoURL] mimeType:@"video/MOV" fileName:@"Video.MOV"];
        [mail setSubject:@"video"];
        [mail setMessageBody:@"body" isHTML:YES];
        [mail setToRecipients:[NSArray arrayWithObject:@"myemail@jhd.com"]];
        [self presentViewController:mail animated:YES completion:nil];
    }else {
        NSLog(@"Device is unable to send the request in its current state.");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM