使用 iPhone 上传视频 [英] Uploading Video with iPhone

查看:22
本文介绍了使用 iPhone 上传视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is it possible to upload video to a server? I know that images are possible. If someone can just point me in the right direction that would be awesome.

谢谢

推荐答案

2015 年 8 月编辑

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

This answer is now seriously out of date. At the time of writing there weren't to many options and videos were relatively small in size. If you are looking at doing this now, I would use AFNetworking which makes this much simpler. It will stream the upload from file rather than holding it all in memory, and also supports the new Apple background upload Task.

此处的文档:https://github.com/AFNetworking/AFNetworking#创建上传任务

--

是的,这是可能的,这就是我的做法.

Yes this is possible and this is how i went about it.

实现以下函数,该函数在媒体选择器完成时运行.

Implement the following function which runs when the media picker is finished.

- (NSData *)generatePostDataForData:(NSData *)uploadData
{
    // Generate the post header:
    NSString *post = [NSString stringWithCString:"--AaB03x
Content-Disposition: form-data; name="upload[file]"; filename="somefile"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary

" 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: [@"
--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];

    }

对于 post 函数,我从其他地方得到了类似的东西(对不起,我不知道我在哪里找到的):

for the post function i had something like this which i got from somewhere else (sorry i dont know where i found it):

- (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");
    }

}

上面的代码片段构建了 http post 请求并提交了它.如果您想要适当的错误处理并考虑使用允许异步上传的库(github 上有一个),则需要对其进行修改

The above snippet builds the http post request and submits it. You will need to modify it if you want decent error handling and consider using a library that allows async upload (theres one on github)

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

Also Notice the port :3000 on the server url above, I found it easy for bug testing to start a rails server on its default port 3000 in development mode so i could see the request parameters for debugging purposes

希望能帮到你

这篇关于使用 iPhone 上传视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆