将视频从库复制到文档文档目录

问题描述 投票:0回答:3

我正在ios应用程序中构建一个任务,我想将视频从库复制到本地应用程序文档目录。我已成功从数组中的库中获取ALAssest列表。现在,当我尝试将任何视频从ALAssest url复制到我们的文件时,我总是收到以下错误

“Error Domain = NSCocoaErrorDomain Code = 262”无法完成操作。 (可可错误262.)“UserInfo = 0x157d4a70 {NSURL = assets-library://asset/asset.m4v?id = B01C37C9-8C8C-4963-BAC8-EE0356C079DD&ext = m4v”

我在文档目录中创建了一个文件夹来存储视频

NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"AppVideo"];
NSError *error=nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath])    //Does directory already exist?
{
    if (![[NSFileManager defaultManager] createDirectoryAtPath:filePath
                                   withIntermediateDirectories:NO
                                                    attributes:nil
                                                         error:&error])
    {
        NSLog(@"Create directory error: %@", error);
    }
    NSLog(@"pathToPatientPhotoFolder filePath=%@",filePath);
}

以下是复制数据的代码

NSError *error=nil;

NSString *path;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"AppVideo"];
path = [path stringByAppendingPathComponent:@"myvideo.mp4"];
NSURL * url=nil;
 url=[[[self.dataContainer objectAtIndex:selected] defaultRepresentation] url];
[[NSFileManager defaultManager] copyItemAtURL:url toURL:[NSURL URLWithString:path] error:&error];

if(error)
    NSLog(@"Writing Error=%@",error.description);
else
    NSLog(@"writing has done without error");
`

我也尝试将ALAssest url的NSData写入文件但是当我用MPMoviePlayerViewController播放它时它只显示黑屏

 ALAssetRepresentation *rep = [[self.dataContainer objectAtIndex:selected] defaultRepresentation];

Byte *buffer = (Byte*)malloc(rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
NSData *FileData = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:NO];


[[NSFileManager defaultManager] createFileAtPath:path
                                        contents:FileData
                                      attributes:nil];

但没有找到任何解决方案。

ios video nsdata alasset file-copying
3个回答
2
投票

真正的问题更深一点。你应该打电话

[NSURL fileURLWithPath:<# string path #>]

代替

[NSURL URLWithString:path]

如果您正在使用文件系统,则必须使用特定的URL。


0
投票

Here你可以看到这是一个与NSFileReadUnsupportedSchemeError = 262相对应的错误。看起来你不能从库移动或复制文件,但是,如果你从库资源获得NSData,使用writeTofile到文件目录中的文件将起作用。


0
投票

最后,我已经解决了将LAtest数据写入我使用的目录路径的问题

 ALAssetRepresentation *rep = [myAssest defaultRepresentation];

Byte *buffer = (Byte*)malloc((NSUInteger)rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:(NSUInteger)rep.size error:nil];
dty = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:NO];
[dty writeToFile:path atomically:YES];

并播放此文件

NSString *path;
NSArray *paths =    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"AppVideo"];
path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mp4",MyString]];

self.videoController = [[MPMoviePlayerController alloc] init];
[self.videoController setContentURL:self.videoURL];
[self.videoController.view setFrame:CGRectMake (0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:self.videoController.view];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(videoPlayBackDidFinish:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:self.videoController];
[self.videoController play];
© www.soinside.com 2019 - 2024. All rights reserved.