1

我想在实时服务器上上传视频,所以请建议我怎么做,代码和教程会更好,而且我是否必须在服务器上制作任何表格来存储数据?

我对存储数据一无所知。

此代码用于上传数据:

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

        //assign the mediatype to a string 
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    //check the media type string so we can determine if its a video
    if ([mediaType isEqualToString:@"public.movie"]){
        NSLog(@"got a movie");
        NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
        webData = [NSData dataWithContentsOfURL:videoURL];
        NSLog(@"webdata is: %@ ",webData);
    }

    [self dismissModalViewControllerAnimated:NO];

    // Handle a movie capture
    if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0)
        == kCFCompareEqualTo) {

        NSString *moviePath = [[info objectForKey:
                                UIImagePickerControllerMediaURL] path];
        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
            UISaveVideoAtPathToSavedPhotosAlbum (moviePath,self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
        } 
    }
}

- (void)video:(NSString*)videoPath didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo
{



    if (error) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Video Saving Failed"  delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil, nil];
        [alert show];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Video Saved" message:@"Saved To Photo Album"  delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [alert show];
    }
}

- (IBAction)uploadVideo:(UIButton*)sender {
    [self post:webData];
    NSLog(@"posted data %@",webData);
}


- (void)post:(NSData *)fileData
{

    NSLog(@"POSTING");

    // Generate the postdata:
    NSData *postData = [self generatePostDataForData: fileData];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    // Setup the request:
    NSMutableURLRequest *uploadRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:SERVER_PATH] cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 30 ];
    [uploadRequest setHTTPMethod:@"POST"];
    [uploadRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [uploadRequest setValue:@"multipart/form-data; boundary=AaB03x" forHTTPHeaderField:@"Content-Type"];
    [uploadRequest setHTTPBody:postData];

    // Execute the reqest:
    NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:uploadRequest delegate:self];
    if (conn)
    {
        // Connection succeeded (even if a 404 or other non-200 range was returned).
        NSLog(@"sucess");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Got Server Response" message:@"Success" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
    else
    {
        // Connection failed (cannot reach server).
        NSLog(@"fail");
    }

}

- (NSData *)generatePostDataForData:(NSData *)uploadData
{
    // Generate the post header:
    NSString *post = [NSString stringWithCString:"--AaB03x\r\nContent-Disposition: form-data; name=\"file\"; filename=\"ios-video.mov\"\r\nContent-Type: video/mov\r\nContent-Transfer-Encoding: binary\r\n\r\n" encoding:NSASCIIStringEncoding];

    // Get the post header int ASCII format:
    NSData *postHeaderData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    // Generate the mutable data variable:
    NSMutableData *postData = [[NSMutableData alloc] initWithLength:[postHeaderData length] ];
    [postData setData:postHeaderData];

    // Add the file:
    [postData appendData: uploadData];

    // Add the closing boundry:
    [postData appendData: [@"\r\n--AaB03x--" dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];

    // Return the post data:
    return postData;
}

我已经像这样定义了我的服务器路径可以吗?

#define SERVER_PATH @"http://manektech.net/diliptest/"

当我记录它时,我的 webdata 输出是这样的:

webdata is: <00000014 66747970 71742020 00000000 71742020 00000008 77696465 0003837f 6d646174 00cc4007 00ec99ee d49b2f4d b44830a9 915555df adeb095c  ...............>
4

2 回答 2

0

NSData当您从相机拍摄视频并在那时停止imagePickerController委托方法调用后,此处在 .h 文件中创建具有 videoData 名称的对象,在这里获取此 URL 或视频,NSData如下所示...

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    { 
        NSLog(@"%@",info);
        [self  dismissModalViewControllerAnimated:YES];

        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[info objectForKey:UIImagePickerControllerMediaURL]]];

        videoData = [[NSData alloc] initWithContentsOfURL:url];
    }

之后,您可以像下面的方法一样发送数据..只需按照我在代码中使用的流程...

-(void)AddVideo{
    NSString *strUSerID = [NSString stringWithFormat:@"%d",userID ];// if required

    NSString *filename = @"massage.mp4";
    NSString *boundary = @"----BOUNDARY_IS_I";
    NSURL *url;
    //&user_id=1&cat_id=1&dap_title=test113434&origin=hi&todap=test343&nottodap=testtt
    NSString *str = SERVER_PATH; // here put your server URL with insert api
    url = [NSURL URLWithString:str];
    [str release];

    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

    [req setHTTPMethod:@"POST"];

    NSString *contentType = [[NSString alloc]initWithFormat:@"multipart/form-data;boundary=%@", boundary];

    [req setValue:contentType forHTTPHeaderField:@"Content-type"];

    [contentType release];

    NSMutableData *postBody = [NSMutableData data];


    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"video\"; filename=\"%@\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"Content-Type: video/mp4\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:videoData];// use here webData which you use for store vedio data
    [req setHTTPBody:postBody];

    NSURLResponse* response;
    NSError* error;
    NSData* result = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
    NSString * rsltStr = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
    NSMutableDictionary *dict;
    if(rsltStr == nil){

    }
    else{
        SBJSON *json = [[SBJSON alloc] init];
        NSError *error = nil;
        dict = [json objectWithString:rsltStr error:&error];
        NSLog(@"%@",dict);
        [json release];
    }
    [rsltStr release];  

    [videoData release];
}

我希望这个答案对你有帮助..

于 2012-10-31T06:15:15.883 回答
0

如果您想通过 TCP 套接字/流来实现,那么请使用NSStream API。- 使用此方法创建一个 NSOutputStream,然后写入缓冲区。

如果你想通过 HTTP 来做,那就去NSURLConnection。在 HTTPBodyStream 中将数据作为 POST 发送。

于 2012-10-31T06:01:27.633 回答