gpt4 book ai didi

ios - 在 iOS 应用程序中保存和显示视频。

转载 作者:行者123 更新时间:2023-11-29 01:46:38 26 4
gpt4 key购买 nike

我正在开发一个应用程序,用户可以在其中录制视频并将其保存在库/或自定义相册中。我可以创建自定义相册并录制视频并将其保存在默认照片库中,即相机胶卷。但我无法将其保存到自定义相册中。另一件事是相册中保存的视频必须显示在应用程序的 Collection View 中,例如图库 View .. 这样用户就可以点击视频进行播放.. 它可能在 GridView 中/ Collection View 或者可能在 UIImage 应该显示相册中保存的视频的 TableView 中..

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

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 的初学者,还在学习中。我已经搜索了一个月了。所以请帮助我。

最佳答案

首先使用以下代码创建相册文件夹并确保将此代码粘贴到 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 - 在 iOS 应用程序中保存和显示视频。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31849342/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com