在 iOS 应用中保存和显示视频。
Posted
技术标签:
【中文标题】在 iOS 应用中保存和显示视频。【英文标题】:Video saving and display in the iOS app. 【发布时间】:2015-08-06 07:25:11 【问题描述】:我正在开发一个应用程序,用户可以在其中录制视频并将其保存在库/或自定义相册中。我可以创建自定义相册并录制视频并将其保存在默认照片库(即相机胶卷)中。但我无法将其保存到自定义相册中。另一件事是相册中保存的视频必须显示在应用程序的集合视图中,即像画廊视图一样。这样用户就可以点击视频进行播放。它可能在网格视图中/ 集合视图或可能在 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】:首先使用以下代码创建您的相册文件夹并确保将此代码粘贴到 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]];
我希望这行得通。 . .
【讨论】:
它显示错误..因为我是iOS的初学者..所以请帮助我。 从链接下载文件:github.com/Kjuly/ALAssetsLibrary-CustomPhotoAlbum/tree/dev/… 并在您的项目中添加 .h 和 .m 文件。 在您的 .h 文件中导入 #import 并在下面的代码行中写入:@property (strong, atomic) ALAssetsLibrary* library;以上是关于在 iOS 应用中保存和显示视频。的主要内容,如果未能解决你的问题,请参考以下文章