视频保存并在iOS应用中显示。

时间:2015-08-06 07:25:12

标签: ios objective-c iphone

我正在开发一个应用,用户可以在其中录制视频并将其保存在库/或自定义相册中。我可以创建自定义相册并录制视频并将其保存在默认照片库中,即相机胶卷..但我无法将其保存到自定义相册中。和其他的东西是相册中保存的视频必须显示在应用程序的集合视图中,即像图库视图..这样用户就可以点击播放的视频..它可能在网格视图中/集合视图或可能在表格视图中UIImage应该显示保存在相册中的视频..

这是我在应用开发中用于创建自定义相册的代码部分。

ALAssetsLibrary* libraryFolder = [[ALAssetsLibrary alloc] init];
[libraryFolder addAssetsGroupAlbumWithName:@"HEP" resultBlock:^(ALAssetsGroup *group)
 {
     NSLog(@"Adding Folder:'My Album', success: %s", group.editable ? "Success" : "Already created: Not Success");
 } failureBlock:^(NSError *error)
 {
     NSLog(@"Error: Adding on Folder");
 }];

用于在应用中录制视频。

- (IBAction)StartRecord:(id)sender {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
    [self presentViewController:picker animated:YES completion:NULL];

}

将其保存在相册中,即默认相册不是我创建的自定义相册。

-(BOOL)startMediaBrowserFromViewController:(UIViewController*)controller usingDelegate:(id )delegate {
    // 1 - Validations
    if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum] == NO)
        || (delegate == nil)
        || (controller == nil)) {
        return NO;
    }
    // 2 - Get image picker
    UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];
    mediaUI.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    mediaUI.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
    // Hides the controls for moving & scaling pictures, or for
    // trimming movies. To instead show the controls, use YES.
    mediaUI.allowsEditing = YES;
    mediaUI.delegate = delegate;
    // 3 - Display image picker
    [controller presentModalViewController:mediaUI animated:YES];
    return YES;
}

因为我在iOS的初学者还在学习..我现在正在搜索一个月。所以请帮助我。

1 个答案:

答案 0 :(得分:1)

首先使用以下代码创建您的相册文件夹&确保此代码粘贴在AppDelegate.m文件中didFinishLaunchingWithOptions方法:

ALAssetsLibrary* libraryFolderSozialConnectVideo = [[ALAssetsLibrary alloc] init];
[libraryFolderSozialConnectVideo addAssetsGroupAlbumWithName:@"HEP" resultBlock:^(ALAssetsGroup *group)
 {
     NSLog(@"HEP Folder Created");
 } failureBlock:^(NSError *error) {
     NSLog(@"Error: Adding on Folder");
 }];

现在,使用以下功能在HEP文件夹中下载您的视频:

- (void)downloadVideo:(NSURL *)videoURL {
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSLog(@"Downloading Started");
    NSData *urlData = [NSData dataWithContentsOfURL:videoURL];
    if (urlData) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"thefile.mp4"];
        [urlData writeToFile:filePath atomically:YES];
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            [self.library saveVideo:[NSURL fileURLWithPath:filePath] toAlbum:@"HEP" completion:^(NSURL *assetURL, NSError *error)
             {
                 NSLog(@"Success downloaded");
             } failure:^(NSError *error) {
                 NSLog(@"Error : %@", [error localizedDescription]);
                 [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                 [self.navigationController popViewControllerAnimated:YES];
             }];
        });
    }
}); }

对于你的didFinishPickingMediaWithInfo方法中的videoURL调用上面的函数,代码如下:

self.library = [[ALAssetsLibrary alloc] init];
[self downloadVideo:[info valueForKey:UIImagePickerControllerMediaURL]];

我希望这有效。 。