将视频文件从iPhone库上传到FTP服务器

时间:2014-02-24 21:26:29

标签: ios iphone objective-c ftp

我是iPhone开发的完全笨蛋,我的背景是Java和C#,我很难掌握语法(特别是字符串操作)。我正在使用最新版本的XCode和带有iOS 7的iPhone。我试图通过FTP将视频和图像上传到我的网络服务器。我下载了SimpleFTPSample并做了一些搞乱,并设法至少上传了.mov文件,遗憾的是我得到的是一个零字节的文件名。这是我的uploadView.h:

    @interface uploadView : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

{
    UIImagePickerController *videoPicker;
    UIImagePickerController *imagePicker;
    UIImage *image;
    NSString *testing;
    NSURL *vidPath;
    IBOutlet UIImageView *imageView;      
}

-(IBAction)takePhoto;

-(IBAction)chooseExisting;

@end

以下是uploadView.m中的拣货方法:

-(IBAction)takePhoto {

    // Changing takePhoto control to choose a video file instead
    videoPicker = [[UIImagePickerController alloc ] init];
    videoPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    videoPicker.delegate = self;
    videoPicker.allowsEditing = NO;
    videoPicker.videoQuality = UIImagePickerControllerQualityTypeHigh;
    videoPicker.videoMaximumDuration = 30.0f;
    videoPicker.mediaTypes = [NSArray arrayWithObject:@"public.movie"];

    if ([self respondsToSelector:@selector(presentViewController:animated:completion:)])
    {
        [self presentViewController:self->videoPicker animated:YES completion:nil];
    }


}


-(IBAction)chooseExisting{

    imagePicker = [[UIImagePickerController alloc ] init];
    imagePicker.delegate = self;
    [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [self presentViewController:imagePicker animated:YES completion:NULL];

}
-(void) videoPickerController:(UIImagePickerController *)videoPicker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    image= [info objectForKey:UIImagePickerControllerOriginalImage];
    [imageView setImage:image];
    vidPath = [info objectForKey:UIImagePickerControllerMediaURL];
    [self dismissViewControllerAnimated:YES completion:NULL];
 }
-(void) videoPickerControllerDidCancel:(UIImagePickerController *)videoPicker
{

    //[self dismissViewControllerAnimated:YES completion:NULL];
    [self->videoPicker dismissViewControllerAnimated:NO completion:nil];    
}

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


    image= [info objectForKey:UIImagePickerControllerOriginalImage];
    [imageView setImage:image];
    vidPath = [info objectForKey:UIImagePickerControllerMediaURL];
    testing = @"testing";
    NSLog(@"%@", testing);
    NSLog(@"%@", vidPath);
    NSLog(@"%@", testing);
    [self dismissViewControllerAnimated:YES completion:NULL];


}

最后是SimpleFTPSample编辑的发送方方法:

- (void)startSend:(NSString *)filePath
{
    BOOL                    success;
    NSURL *                 url;

    self.username = @"username";
    self.password = @"password";
    self.address = @"ftpaddress";
    assert(filePath != nil);
    assert( [filePath.pathExtension isEqual:@"png"] || [filePath.pathExtension isEqual:@"jpg"] || [filePath.pathExtension isEqual:@"MOV"] || [filePath.pathExtension isEqual:@"mp4"]);
    NSLog(@"%@", @"before assertion test");
    NSLog(@"%@", filePath);
    NSLog(@"%@", @"before assertion test");
    //assert([[NSFileManager defaultManager] fileExistsAtPath:filePath]);

    assert(self.networkStream == nil);      // don't tap send twice in a row!
    assert(self.fileStream == nil);         // ditto

    // First get and check the URL.

    url = [[NetworkManager sharedInstance] smartURLForString:self.address];
    success = (url != nil);

    if (success) {
        // Add the last part of the file name to the end of the URL to form the final
        // URL that we're going to put to.

        url = CFBridgingRelease(
                                CFURLCreateCopyAppendingPathComponent(NULL, (__bridge CFURLRef) url, (__bridge CFStringRef) [filePath lastPathComponent], false)
                                );
        success = (url != nil);
    }

    // If the URL is bogus, let the user know.  Otherwise kick off the connection.

    if ( ! success) {
        self.statusLabel.text = @"Invalid URL";
    } else {

        // Open a stream for the file we're going to send.  We do not open this stream;
        // NSURLConnection will do it for us.

        self.fileStream = [NSInputStream inputStreamWithFileAtPath:filePath];
        assert(self.fileStream != nil);

        [self.fileStream open];

        // Open a CFFTPStream for the URL.

        self.networkStream = CFBridgingRelease(
                                               CFWriteStreamCreateWithFTPURL(NULL, (__bridge CFURLRef) url)
                                               );
        assert(self.networkStream != nil);

        if ([self.username length] != 0) {
            success = [self.networkStream setProperty:self.username forKey:(id)kCFStreamPropertyFTPUserName];
            assert(success);
            success = [self.networkStream setProperty:self.password forKey:(id)kCFStreamPropertyFTPPassword];
            assert(success);
        }

        self.networkStream.delegate = self;
        [self.networkStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
        [self.networkStream open];

        // Tell the UI we're sending.

        [self sendDidStart];
    }
}

另一个:

- (IBAction)sendAction:(UIView *)sender
{
    assert( [sender isKindOfClass:[UIView class]] );

    if ( ! self.isSending ) {
        NSString *  filePath;
        NSString *  trimmedPath;
        NSString *  testAssign;
        NSString *  absString;


        assert(sender.tag >= 0);

        absString = [vidPath absoluteString];
        trimmedPath= [absString stringByReplacingOccurrencesOfString:@"file://" withString:@""];
        filePath = trimmedPath;

        assert(filePath != nil);

        [self startSend:filePath];
    }
}

对不起,如果这是太多的信息,我对语言和SDK的无知是罪魁祸首。请不要回复以下评论:&#34;了解有关iPhone网络的更多信息。&#34;等等,我知道我应该,但我很快就会开始一份新工作,时间已经不多了,无法完成这个应用程序。任何帮助将不胜感激。我主要关注视频上传而不是图片。

1 个答案:

答案 0 :(得分:0)

事实证明,这段代码工作正常,只会让我在模拟器上遇到麻烦。