通过http上传视频

时间:2014-04-28 14:16:25

标签: ios objective-c http nsdata

所以这是我的尝试:

我首先创建一个“图像选择器”,它允许我拍摄视频:

- (IBAction)takeVideo:(UIButton *)sender {

    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];

    [self presentViewController:picker animated:YES completion:NULL];

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

    self.movieURL = info[UIImagePickerControllerMediaURL];
        [picker dismissViewControllerAnimated:YES completion:NULL];

}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {

    [picker dismissViewControllerAnimated:YES completion:NULL];

}
- (void)viewDidAppear:(BOOL)animated {

    self.movieController = [[MPMoviePlayerController alloc] init];
    self.movieController.controlStyle = MPMovieControlStyleNone;
    [self.movieController setContentURL:self.movieURL];
    [self.movieController.view setFrame:CGRectMake (15, 125, 292, 390)];
    self.movieController.view.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.movieController.view];
    [self.movieController play];

    NSString *myString = [self.movieURL absoluteString];
    //THE DATA IS RETURNING 0 :
    NSData *videoData = [NSData dataWithContentsOfFile:myString];
    NSLog(@"%@",[NSByteCountFormatter stringFromByteCount:videoData.length countStyle:NSByteCountFormatterCountStyleFile]);
    [self setImageDataToSend:videoData];

}

- (void)moviePlayBackDidFinish:(NSNotification *)notification {

    [[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

    [self.movieController stop];
    [self.movieController.view removeFromSuperview];
    self.movieController = nil;

}

我没有从网址收到任何数据?

然后我尝试将其上传到我的网站:

-(IBAction)uploadvideotowebsite:(id)sender{
    //you can use any string instead "com.mycompany.myqueue"
    dispatch_queue_t backgroundQueue = dispatch_queue_create("com.mcompany.myqueue", 0);
    //turn on
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    dispatch_async(backgroundQueue, ^{


    PFUser *user = [PFUser currentUser];
    NSString * url = [NSString stringWithFormat:@"http://******.co.uk/****/imageupload.php"];
    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"POST"];
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSMutableData *body = [[NSMutableData alloc]init];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    NSString *String = @"Content-Disposition: form-data; name=\"image\"; filename=\"";
    NSString *String2 = @"video";
    NSString *String3 = @".MOV\"\r\n";
    NSString *connection1 = [String stringByAppendingString: String2];
    NSString *done = [connection1 stringByAppendingString: String3];
    [body appendData:[[NSString stringWithFormat:done] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[@"Content-Type: audio/basic\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:_imageDataToSend];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:body];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    //Send the Request
    NSData* returnData = [NSURLConnection sendSynchronousRequest: request
                                               returningResponse: nil error: nil];
    //serialize to JSON
    if (!returnData){
        return; // or handle no connection error here
    }else{
        NSDictionary *result = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:nil];

        //parsing JSON
        bool success = [result[@"success"] boolValue];
        if(success){
            NSLog(@"Success=%@",result[@"msg"]);
            //[self performSegueWithIdentifier:@"toshow" sender:self];
        }else{
            NSLog(@"Fail=%@",result[@"msg"]);
        }
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        //turn off
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    });
});


}

我没有收到任何错误。但我在我的网站上收到一个大小为0kb的文件。

有人可以解释为什么我没有从视频中获取数据吗?

1 个答案:

答案 0 :(得分:1)

有人可以解释为什么我没有从视频中获取数据吗?

当然,这是你的错误:

 NSString *myString = [self.movieURL absoluteString];

应该是路径

NSString *myString = [self.movieURL path];
NSData *videoData = [NSData dataWithContentsOfFile:myString];
NSLog(@"%@",[NSByteCountFormatter stringFromByteCount:videoData.length countStyle:NSByteCountFormatterCountStyleFile]);