首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >录制的视频路径无法上传到iPhone服务器

录制的视频路径无法上传到iPhone服务器
EN

Stack Overflow用户
提问于 2013-11-13 09:00:45
回答 1查看 1.3K关注 0票数 0

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

我采用了PBJVision自定义视频录制,用于录制Vine应用程序等选项。我会集成到我的应用程序中。

首先,我像下面这样的"/var/mobile/Applications/9C4A08BC-9190-4A37-A875-3077236731AB/Documents/13-11-2013||05:54:78.mp4".从PBJVision类文件中获得了路径值

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

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

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

代码语言:javascript
复制
 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);

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

Nov 13 05:55:41 Lion-Users-iPod Vision17484 : return data <0909>

Nov 13 05:55:41 Lion-Users-iPod Vision17484 :来自服务器is=的响应

我不知道为什么我会得到这个空值。因为路径不是空值,所以在我使用MPMoviePlayercontroller将该路径用于显示预览之前。

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

你能指导我如何在服务器上上传视频文件以及我哪里出了错吗?

EN

回答 1

Stack Overflow用户

发布于 2015-06-05 17:35:13

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

代码语言:javascript
复制
println(NSFileManager.defaultManager().fileExistsAtPath( urlvalue.path!))

O/P you will get => False

在阅读了整个IOS文档后,我也在几天前解决了这个问题。我认为“我们只能在打开PhotoAlbum会话的情况下才能访问PHImageManager Assets”。要交叉检查此语句,请尝试下面的代码-:

代码语言:javascript
复制
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会话之后,当我尝试使用路径访问视频时。它工作得很好。还可以使用相册中视频的相对路径在后台成功上传所有视频文件。

如果你需要,我可以把我的实现发送给你。但是不确定这是不是正确的方式。但对我来说工作得很好。

希望能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19943253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档