3

我正在从 iPad 应用程序录制视频,我希望该视频可以保存在文档文件夹中,或者我们可以直接将其上传到服务器。我已将音频文件存储在文档中,但如何保存视频文件。我正在使用以下代码录制视频。

  UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  picker.delegate = self;

  if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
  {
    NSArray *mediaTypes = [NSArray arrayWithObject:(NSString*)kUTTypeMovie];
    picker.mediaTypes = mediaTypes ;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo ;

    [self presentModalViewController:picker animated:NO];

    [picker release];
}
  else
    {

UIAlertView *alt=[[UIAlertView alloc]initWithTitle:@"Error" message:@" Camera Facility is not available with this Device" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alt show];
    [alt release];
    }
4

3 回答 3

12

试试这个,我用当前的日期时间存储它::

-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
    [self dismissViewControllerAnimated:NO completion:nil];
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];

    if ([type isEqualToString:(NSString *)kUTTypeVideo] || [type isEqualToString:(NSString *)kUTTypeMovie])
    {
        videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

        NSLog(@"found a video");

        // Code To give Name to video and store to DocumentDirectory //

        videoData = [[NSData dataWithContentsOfURL:videoURL] retain];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];

        NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
        [dateFormat setDateFormat:@"dd-MM-yyyy||HH:mm:SS"];
        NSDate *now = [[[NSDate alloc] init] autorelease];
        theDate = [dateFormat stringFromDate:now];

        NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Default Album"];

        if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
           [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:nil];

        NSString *videopath= [[[NSString alloc] initWithString:[NSString stringWithFormat:@"%@/%@.mov",documentsDirectory,theDate]] autorelease];

        BOOL success = [videoData writeToFile:videopath atomically:NO];

        NSLog(@"Successs:::: %@", success ? @"YES" : @"NO");
        NSLog(@"video path --> %@",videopath);
    }
}

视频上传 ::

videoData是从videoData = [[NSData dataWithContentsOfURL:videoURL] retain];

- (void)uploadVideo
{
    NSData *imageData = videoData;

    NSString *urlString=[NSString stringWithFormat:@"%s", UploadVideoService];
    NSLog(@"url=== %@", urlString);

    request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    /*  body of the post */

    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    //Video Name with Date-Time
    NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"yyyy-MM-dd-hh:mm:ssa"];
    NSString *currDate = [dateFormat stringFromDate:[NSDate date]];

    NSString *str = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"video-%@.mov\"\r\n", currDate];
    NSLog(@"String name::  %@",str);

    [dateFormat release];

    [body appendData:[[NSString stringWithString:str] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [request setHTTPBody:body];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(@"result from webservice:::--> %@", returnString);

    [returnString release];
}

希望,它会帮助你。

谢谢。

于 2013-03-20T08:55:22.450 回答
1

一旦像这样尝试,

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

    NSURL *videoUrl = (NSURL *)[info objectForKey:UIImagePickerControllerMediaURL]; 

    NSDate *now = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = @"hh:mm:ss";
    [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];

    NSData *videoData = [NSData dataWithContentsOfURL:videoUrl];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *savedvedioPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:now]]];
    savedvedioPath  = [savedvedioPath stringByAppendingFormat:@".mp4"];
    [videoData writeToFile:savedvedioPath atomically:NO];

    //here is the method to upload onto server
    [self Upload_server:savedvedioPath];

    [self dismissModalViewControllerAnimated:YES];
}    

现在定义您上传视频的方法,例如,

-(void)Upload_server:(NSString*)file_path {

  NSURL *url = [NSURL URLWithString: @"YOUR_URL_TO_UPLOAD"];
  ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
  [request setUseKeychainPersistence:YES];
  [request addFile:file_path forKey:@"YOUR_KEY"];

//insted of in above line you can also use [request setData:vedioData withFileName:@"your_file_name" andContentType:@"video/mp4" forKey:@"YOUR_KEY"] by Sending  vedioData of type NSData as another perameter to this method.

[request setDelegate:self];
[request setDidFinishSelector:@selector(uploadRequestFinished:)];
[request setDidFailSelector:@selector(uploadRequestFailed:)];
[request startAsynchronous];

}

现在实现 ASIFormDataRequest 委托方法,例如,

- (void)uploadRequestFinished:(ASIHTTPRequest *)request{ 

   NSString *responseString = [request responseString];
  //do something after sucessful upload

}

- (void)uploadRequestFailed:(ASIHTTPRequest *)request{

  NSLog(@" Error - Statistics file upload failed: \"%@\"",[[request error] localizedDescription]); 

  }

在这里,我将 ASIFormDataRequest 上传到服务器。希望它会帮助你..

于 2013-03-20T09:21:16.290 回答
0
//for video..
#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/Mediaplayer.h>

#import <CoreMedia/CoreMedia.h>



UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        NSArray *mediaTypes = [NSArray arrayWithObject:(NSString*)kUTTypeMovie];
        picker.mediaTypes = mediaTypes ;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo ;

        [self presentModalViewController:picker animated:NO];

        [picker release];
    }
    else
    {
        UIAlertView *alt=[[UIAlertView alloc]initWithTitle:@"Error" message:@" Camera Facility is not available with this Device" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alt show];
        [alt release];
    }

用于保存到 Document 文件夹并保存在照片库中

NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];


        //for video
        NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
        NSLog(@"video url-%@",videoURL);

        NSData *videoData = [NSData dataWithContentsOfURL:videoURL];

        NSString * videoName = [NSString stringWithFormat:@"student_%d_%d.mp4",stud_id,imgVidID];

        videoPath = [documentsDirectory stringByAppendingPathComponent:videoName];

        NSLog(@"video path-%@",videoPath);

        [videoData writeToFile:videoPath atomically:YES];

        NSString *sourcePath = [[info objectForKey:@"UIImagePickerControllerMediaURL"]relativePath];

        UISaveVideoAtPathToSavedPhotosAlbum(sourcePath,nil,nil,nil);
}
于 2013-03-20T09:10:30.490 回答