用iPhone上传视频

7 iphone video upload

是否可以将视频上传到服务器?我知道图像是可能的.如果有人能指出我正确的方向,那将是非常棒的.

谢谢

ADA*_*DAM 17

2015年8月编辑

这个答案现在已经过时了.在撰写本文时,没有太多选项,视频的尺寸相对较小.如果您现在正在考虑这样做,我会使用AFNetworking它使这更简单.它将从文件中流式传输上传而不是将其全部保存在内存中,并且还支持新的Apple后台上传任务.

文档:https://github.com/AFNetworking/AFNetworking#creating-an-upload-task

-

是的,这是可能的,这就是我的方式.

实现以下在媒体选择器完成时运行的功能.

- (NSData *)generatePostDataForData:(NSData *)uploadData
{
    // Generate the post header:
    NSString *post = [NSString stringWithCString:"--AaB03x\r\nContent-Disposition: form-data; name=\"upload[file]\"; filename=\"somefile\"\r\nContent-Type: application/octet-stream\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 image:
    [postData appendData: uploadData];

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

    // Return the post data:
    return postData;
}


- (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];
        NSData *webData = [NSData dataWithContentsOfURL:videoURL];
        [self post:webData];
        [webData release];

    }
Run Code Online (Sandbox Code Playgroud)

对于帖子功能我有这样的东西,我从其他地方得到(抱歉,我不知道我在哪里找到它):

- (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:@"http://www.example.com:3000/"] cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 30 ] autorelease];
    [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];
        [alert release];
    }
    else
    {
        // Connection failed (cannot reach server).
        NSLog(@"fail");
    }

}
Run Code Online (Sandbox Code Playgroud)

上面的代码段构建了http post请求并提交它.如果你想要正确的错误处理并考虑使用允许异步上传的库(在github上有一个),你将需要修改它

另请注意上面的服务器URL上的端口:3000,我发现在开发模式下,在默认端口3000上启动rails服务器很容易进行错误测试,因此我可以看到用于调试目的的请求参数

希望这可以帮助


小智 2

看一下 UIImagePickerController。从 3.0 开始,您可以选择拍摄视频或选择现有视频。根据文档,您观看电影的时长最多为 10 分钟:

http://developer.apple.com/IPhone/library/documentation/UIKit/Reference/UIImagePickerController_Class/UIImagePickerController/UIImagePickerController.html