ios - 通过http上传视频

标签 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;
    
}

虽然我没有收到来自 URL 的任何数据?

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

-(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 的文件。

谁能解释为什么我没有从视频中获取数据?

最佳答案

谁能解释为什么我没有从视频中获取数据?

当然,这是你的错误:

 NSString *myString = [self.movieURL absoluteString];

应该是路径

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

关于ios - 通过http上传视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23343491/

相关文章:

ruby - 使用 Ruby 的 HTTP 服务器

ios - 在 iPhone 上运行 React Native 应用程序

ios - 无法在 Xcode 中查看配置文件

objective-c - 如何使用核心数据进行依赖注入(inject)

ios - 当应用程序处于后台时播放音频

现有数据的 REST 响应代码

http - 如何为云端设置缓存 header 以便 Google Pagespeed 识别它?

ios - 在我的应用程序 UIActivityViewController 中弹出类似 Safari 的微信/微信分享表?

ios - 图像优化 (iOS)

ios - 如何通过延迟 iOS 上的 block 调用来避免保留周期