从我的应用程序将视频文件上传到 ftp 服务器

Posted

技术标签:

【中文标题】从我的应用程序将视频文件上传到 ftp 服务器【英文标题】:uploading video file to ftp server from my app 【发布时间】:2014-08-22 05:13:57 【问题描述】:

我正在尝试将我的视频文件从我的应用程序上传到服务器,并且在 uploadfilewitherror 处的此代码中出现异常。

2014-08-22 15:55:49.092 [1990:907] -[__NSCFNumberlocalizedDescription]:无法识别的选择器发送到实例 0x1f58fd60 2014-08-22 15:55:49.096 [1990:907] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSCFNumber 本地化描述]:无法识别的选择器发送到实例 0x1f58fd60” * 首先抛出调用栈: (0x326ed2a3 0x3a36b97f 0x326f0e07 0x326ef531 0x32646f68 0x74bdd 0x7328f 0x32f82d41 0x32f7a5c1 0x32ff2be3 0x3a78311f 0x3a7824b7 0x3a783dcb 0x326c0f3b 0x32633ebd 0x32633d49 0x361e62eb 0x34549301 0x6fac3 0x6fa50) libc++abi.dylib:终止调用抛出异常

Code is as below.


 - (void)viewDidLoad
         
        [super viewDidLoad];
        ftpRequest = [[SCRFTPRequest alloc] init];
        // Do any additional setup after loading the view, typically from a nib.



        

    - (void)viewDidUnload
    
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    

    - (void)viewWillAppear:(BOOL)animated
    
        [super viewWillAppear:animated];
    

    - (void)viewDidAppear:(BOOL)animated
    
        [super viewDidAppear:animated];
    

    - (void)viewWillDisappear:(BOOL)animated
    
        [super viewWillDisappear:animated];
    

    - (void)viewDidDisappear:(BOOL)animated
    
        [super viewDidDisappear:animated];
    

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    
        // Return YES for supported orientations
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
            return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
         else 
            return YES;
        
    


    - (void) recordVideo 

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


        NSArray *sourceTypes = 
        [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
        if (![sourceTypes containsObject:(NSString *)kUTTypeMovie ])
            NSLog(@"Can't save videos");
        



        [self presentModalViewController:picker animated:YES];
        [picker release];   
    


    - (IBAction)startRecordClick:(id)sender 
        [self recordVideo];
    



    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo 

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



        NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

        if (![mediaType isEqualToString:kUTTypeMovie])
            return;

        NSURL *mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
        NSString* moviePath = mediaURL.absoluteString;
        NSString *tempFilePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
        NSLog(@"filepath %@",tempFilePath);

        //try

        ftpRequest = [[SCRFTPRequest alloc] initWithURL:[NSURL URLWithString:@"ftp://"]
                                           toUploadFile:[[NSBundle mainBundle] pathForResource:@"tempFilePath" ofType:@"MOV"]];

        ftpRequest.username = @"";
        ftpRequest.password = @"";

        // Specify a custom upload file name (optional)
        ftpRequest.customUploadFileName = @"c.MOV";

        // The delegate must implement the SCRFTPRequestDelegate protocol
        ftpRequest.delegate = self;
        [ftpRequest startRequest];

        //try

        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(tempFilePath))
        
            UISaveVideoAtPathToSavedPhotosAlbum (tempFilePath, nil, nil, nil);   
        

        [picker  dismissModalViewControllerAnimated: YES];
        [picker release];

    





    -(void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
        NSLog(@"Finished with error: %@", error);
    

    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
        [picker  dismissModalViewControllerAnimated: YES];
        [picker release];

    


    - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated 
    
        - (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated 

        

    - (void)ftpRequestDidFinish:(SCRFTPRequest *)request 

        NSLog(@"Upload finished.");
    

    - (void)ftpRequest:(SCRFTPRequest *)request didFailWithError:(NSError *)error 

        NSLog(@"Upload failed: %@", [error localizedDescription]);
    

    // Optional delegate methods
    - (void)ftpRequestWillStart:(SCRFTPRequest *)request 

        NSLog(@"Will transfer %lld bytes.", request.fileSize);
    

    - (void)ftpRequest:(SCRFTPRequest *)request didWriteBytes:(NSUInteger)bytesWritten 

        NSLog(@"Transferred: %d", bytesWritten);
    

    - (void)ftpRequest:(SCRFTPRequest *)request didChangeStatus:(SCRFTPRequestStatus)status 

        switch (status) 
            case SCRFTPRequestStatusOpenNetworkConnection:
                NSLog(@"Opened connection.");
                break;
            case SCRFTPRequestStatusReadingFromStream:
                NSLog(@"Reading from stream...");
                break;
            case SCRFTPRequestStatusWritingToStream:
                NSLog(@"Writing to stream...");
                break;
            case SCRFTPRequestStatusClosedNetworkConnection:
                NSLog(@"Closed connection.");
                break;
            case SCRFTPRequestStatusError:
                NSLog(@"Error occurred.");
                break;
            case SCRFTPRequestStatusNone:
                NSLog(@"Error occurred - NONE.");
                break;

        
    


    @end

【问题讨论】:

【参考方案1】:

提供的代码没有任何与 FTP 服务器相关的代码库。

didFinishPickingMediaWithInfo 委托将在您从选择器控制器使用/完成视频选择时触发。如果您想同时将视频上传到 FTP,无需任何按钮操作,只需使用以下代码即可。

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


NSURL *mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];

NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
//search for FTP code samples and upload this NSData to the FTP.

    //save this video
    NSString *moviePath = [mediaURL path];

    if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) 
        UISaveVideoAtPathToSavedPhotosAlbum (moviePath, nil, nil, nil);
    


如果您想对将视频发送到 ftp 服务器的按钮进行其他操作,请保存资产 URL 并使用 ALAsset Url 检索视频文件,将其转换为 strems 并上传到 FTP。

【讨论】:

非常感谢,我坚持这个要求,我会尝试这个方法并回复你。 ,如何获取录制视频的videoURL? 我已经完成了我的编码并且我在代码中遇到了一个异常,我已经编辑了我的问题,请你看一下代码并告诉我需要做的更改。

以上是关于从我的应用程序将视频文件上传到 ftp 服务器的主要内容,如果未能解决你的问题,请参考以下文章

让用户将视频从我的站点服务器上传到他们的 youtube 频道

怎么通过PHP来支持FTP和HTTP上传

从我的云存储中自动创建和上传 Youtube 视频

自己创建网页,怎样才能上传视频到自己的网页呢?

将实时视频从我的 c# 应用程序流式传输到 ASP.NET 网页

如何在网页实现上传各种文件或图片视频等功能