我正在ios app中构建一个任务,我想将视频从库复制到本地应用文档目录。 我已成功从数组中的库中获取ALAssest列表。 现在,当我尝试将任何视频从ALAssest url复制到我们的文件时,我总是收到以下错误
"错误域= NSCocoaErrorDomain代码= 262"操作不能 完成。 (可可错误262。)"的UserInfo = 0x157d4a70 {NSURL =资产库://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];
但没有找到任何解决方案。
答案 0 :(得分:2)
真正的问题更深一点。你应该打电话给
[NSURL fileURLWithPath:<# string path #>]
而不是
[NSURL URLWithString:path]
如果您正在使用文件系统,则必须使用特定的网址。
答案 1 :(得分:0)
Here您可以看到它是与NSFileReadUnsupportedSchemeError = 262
对应的错误。看起来您无法从库中移动或复制文件,但是,如果从库资产中获取NSData
,则使用writeTofile
到文档目录中的文件将起作用。
答案 2 :(得分: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];