【问题标题】:Recorded video path can't upload on server for iPhone录制的视频路径无法上传到 iPhone 的服务器上
【发布时间】:2013-11-25 10:23:18
【问题描述】:

现在我尝试在网络服务器上上传录制视频路径文件。但是在上传设备捕获的录制视频时,我遇到了一些严重错误。

我已将 PBJVision 自定义视频录制用于 Vine 应用等录制选项。然后我集成到我的应用中。

首先我从 PBJVision 类文件中获取路径值,如下所示“/var/mobile/Applications/9C4A08BC-9190-4A37-A875-3077236731AB/Documents/13-11-2013||05:54:78.mp4” .

这里我使用 NSDateFormat 来识别制作不同的视频。

然后我使用上述路径的修剪字符功能。然后我将此路径更改为 NSURL 格式。

然后我使用下面的代码使用上传视频路径文件,

 NSURL *urlvalue = [NSURL fileURLWithPath:self.videoPath];
    NSLog(@"The urlvalue is = %@",urlvalue);


    NSData *urldata = [NSData dataWithContentsOfURL:urlvalue];

     //NSURL *fileURL    =   [NSURL fileURLWithPath:videoPath];
     //NSLog(@"fileurl is = %@",fileURL);

     //NSData *videoData1 = [videoPath dataUsingEncoding:NSUTF8StringEncoding];
     //NSData *videoData1=[NSData dataWithContentsOfFile:videoPath];
    NSData *videoData1=urldata;
     //NSLog(@"URL FOR VIDEO = %@",videoData);
     NSString *postLength = [NSString stringWithFormat:@"%d", [videoData1 length]];

     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
     [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://demo.xyzxyzxyz.com/client/vine-clone/mobile/video_upload"]]];


     [request setHTTPMethod:@"POST"];
     [request setTimeoutInterval:60000];


     NSString *boundary = [NSString stringWithFormat:@"---------------------------14737809831466499882746641449"];
     [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
     NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
     [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

     NSMutableData *body = [NSMutableData data];

     //video
     [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
     //[body appendData:[@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"video.mp4\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

    //Video Name with Date-Time
    NSDateFormatter *dateFormat=[[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"yyyy-MM-dd-hh:mm:ssa"];
    NSString *currDate = [dateFormat stringFromDate:[NSDate date]];
    NSString *str = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"video-%@.mov\"\r\n", currDate];
    NSLog(@"String name::  %@",str);

    [body appendData:[[NSString stringWithString:str] dataUsingEncoding:NSUTF8StringEncoding]];

     //[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"video.mp4\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
     //[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
     [body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
     [body appendData:[NSData dataWithData:videoData1]];
     //[body appendData:videoData];
     [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

     //set the body to the request
     [request setHTTPBody:body];

     // send the request
     NSError *error13;
     NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error13];

     if(error13 != nil)
     {
     NSLog(@"erro is = %@",error13);
     }

     NSLog(@"return data %@",returnData);
     NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

     NSLog(@"response from server is= %@",returnString);

这里我只得到结果字符串是,

11 月 13 日 05:55:41 Lion-Users-iPod Vision[17484]:返回数据

11 月 13 日 05:55:41 Lion-Users-iPod Vision[17484]:来自服务器的响应是=

我不知道为什么我得到这个空值。因为路径不是空值,所以在此之前我使用 MPMoviePlayercontroller 使用该路径进行放映预览。

玩的不错。我检查了一些 video.mp4 文件作为一个包,并创建了上面代码使用的本地路径。它是使用上述格式从系统编译器和设备上传的。

您能否指导我如何在服务器上上传视频文件以及我出错的地方。

【问题讨论】:

    标签: ios iphone video upload


    【解决方案1】:

    拉曼这是 IOS 设备的经典用例。它不允许您直接使用路径值访问任何相册资产。要交叉检查我的答案,请检查您的文件的 FileExistsAtPath,如下所示 -:

    println(NSFileManager.defaultManager().fileExistsAtPath( urlvalue.path!))
    
    O/P you will get => False
    

    在阅读了整个 IOS 文档后,几天前我也遇到了这个问题。我已经弄清楚了“当且仅当我们打开 PHImageManager 会话时,我们才能访问 PhotoAlbum 资产”。要交叉检查此语句,请尝试以下代码 -:

    var currentVideofetch: PHFetchResult!
    required init(coder aDecoder: NSCoder) {
        let options = PHFetchOptions()
        options.sortDescriptors = [
            NSSortDescriptor(key: "creationDate", ascending: true)
        ]
        currentVideofetch = PHAsset.fetchAssetsWithMediaType(.Video, options: options)
        super.init(coder: aDecoder)
    }
    func checkImageExists(){
    let asset = self.currentVideofetch.objectAtIndex(1) as? PHAsset
    }
    if let checkedAsset = asset {
            PHImageManager.defaultManager().requestAVAssetForVideo(checkedAsset, options: nil, resultHandler: {[weak self](result: AVAsset!, audioMix: AVAudioMix!, info: [NSObject : AnyObject]!) in
                println(NSFileManager.defaultManager().fileExistsAtPath(self.urlvalue.path!))
                })
        }
    O/P you will get => True
    

    打开 PHImageManager 会话后,然后当我尝试使用路径访问视频时。它工作得很好。还可以使用相册中视频的相对路径成功上传后台所有视频文件。

    如果您需要,我可以将我的实现发送给您。但不确定它是否正确。但对我来说工作正常。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-25
      • 2021-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-14
      • 2013-04-26
      相关资源
      最近更新 更多