从 NSBundle 硬编码视频迁移到允许图像选择器文件

Migrating from NSBundle hardcoded video to allow for image picker file instead

我是新来的,还处于起步阶段,所以请多多包涵。

我有代码可以上传嵌入在项目中的 "videoTest.MOV" 文件。我想更改该代码以上传我从下面视频操作中包含的图像选择器代码中选择的视频。

感谢您的帮助,我完全被这个难住了。

// Upload static file to Youtube
-(void)uploadVideoOnYouTube
{
_uploadVideo = [[YouTubeUploadVideo alloc] init];
_uploadVideo.delegate = self;
NSURL *VideoUrl = [[NSBundle mainBundle] URLForResource:@"videoTest" withExtension:@"MOV"];
NSData *fileData = [NSData dataWithContentsOfURL:VideoUrl];
NSString *title;
NSString *description;

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"'Test Video Uploaded ('EEEE MMMM d, YYYY h:mm a, zzz')"];
title = [dateFormat stringFromDate:[NSDate date]];

description = @"This is test";

[self.uploadVideo uploadYouTubeVideoWithService:self.youtubeService
                                       fileData:fileData
                                          title:title
                                    description:description];
}

// Image Picker that only shows movies, no images.
- (IBAction)video:(id)sender {

UIImagePickerController *imagePicker = [[UIImagePickerController alloc]     init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie,      nil];

[self presentModalViewController:imagePicker animated:YES];
}

在本地保存视频,您需要什么格式,您可以将其发送到您的服务器

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

    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
    NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *tempPath = [documentsDirectory stringByAppendingFormat:@"/vid1.mp4"];
    BOOL success = [videoData writeToFile:tempPath atomically:NO];
    [picker dismissViewControllerAnimated:YES completion:nil];
    }

注:未测试

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

// This is the NSURL of the video object
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

NSLog(@"VideoURL = %@", videoURL);

[picker dismissViewControllerAnimated:YES completion:NULL];
[self YoutubeShare:videoURL];
}