使用目标c上传视频

时间:2011-11-18 06:21:42

标签: objective-c ios

我现在要构建一个非常复杂(至少对我来说)的应用程序。基本应用程序如下:

标签栏应用,带有两个标签,一个用于本地视频,另一个用于流式传输视频。

在这个阶段很简单,但现在我必须上传视频(从iphone图库或用户拍摄)到服务器。

真的不知道该怎么做。请帮帮我。

2 个答案:

答案 0 :(得分:9)

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
    NSURL *urlvideo = [info objectForKey:UIImagePickerControllerMediaURL];


    urlvideo contains the URL of that video file that has to be uploaded. Then convert the url into NSString type because setFile method requires NSString as a parameter

    NSString *urlString=[urlvideo path];

    NSLog(@"urlString=%@",urlString);
    NSString *str = [NSString stringWithFormat:@"path of server"];
    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setFile:urlString forKey:@"key foruploadingFile"];
    [request setRequestMethod:@"POST"];
    [request setDelegate:self];
    [request startSynchronous];
    NSLog(@"responseStatusCode %i",[request responseStatusCode]);
    NSLog(@"responseStatusCode %@",[request responseString]);
    }

希望这段代码可以节省您的几个小时。

答案 1 :(得分:0)

使用AFNetworking:

NSURL *url = [NSURL URLWithString:@"http://api-base-url.com"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"], 0.5);
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
}];