Ios 将UISaveVideoAtPathToSavedPhotosAlbum@selector videoSaved函数替换为 Xcode 7 斯威夫特2 目标C
我修改了一些Objective C代码,但我根本不知道Objective C语法。我已替换了将视频保存到相机卷的以下代码:Ios 将UISaveVideoAtPathToSavedPhotosAlbum@selector videoSaved函数替换为 Xcode 7 斯威夫特2 目标C,ios,objective-c,Ios,Objective C,我修改了一些Objective C代码,但我根本不知道Objective C语法。我已替换了将视频保存到相机卷的以下代码: if ( UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(newPath)) // Copy it to the camera roll. UISaveVideoAtPathToSavedPhotosAlbum(newPath, self, @selector(videoSaved:didFinishSavingW
if ( UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(newPath))
// Copy it to the camera roll.
UISaveVideoAtPathToSavedPhotosAlbum(newPath, self, @selector(videoSaved:didFinishSavingWithError:contextInfo:), (__bridge void *)(AvailableVideos[0]));
else
{
[self ErrorOnDownloadOrSave];
return;
}
使用将视频保存到我的应用程序的数据容器文档目录的代码:
NSString *tempFilePath = [downloadURL path];
NSError * error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/Videos"];
//NSString *newPath = [[tempFilePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:AvailableVideos[0]];
NSString *newPath = [dataPath stringByAppendingPathComponent:AvailableVideos[0]];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:dataPath])
[fileManager createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
//copying temp video to Documents Directory
if ([fileManager fileExistsAtPath:newPath] == YES)
[fileManager removeItemAtPath:newPath error:&error];
[fileManager copyItemAtPath:tempFilePath toPath:newPath error:&error];
在保存到iOS摄像机卷的目标C代码中,有一个目标C“函数”,在“videoSaved”事件完成时调用:
-(void)videoSaved:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
//implementation
}
我需要弄清楚如何在以下代码后调用此“函数”、“videoSaved”:
//copying temp video to Documents Directory
if ([fileManager fileExistsAtPath:newPath] == YES)
[fileManager removeItemAtPath:newPath error:&error];
[fileManager copyItemAtPath:tempFilePath toPath:newPath error:&error];
我意识到videoSaved
函数是uisavevideoatpathtosavedphotosalebum
的一个特殊签名,但我对Objective C太不熟悉了,我不知道如何编写一个可以调用的新函数,并传递my error对象并维护videoSaved
的实现。根据文档,方法copyItemAtPath
返回BOOL
如果项目复制成功或文件管理器的委托故意停止操作,则为“是”。如果发生错误,则返回NO
因此,您可以执行以下操作:
if ([fileManager copyItemAtPath:tempFilePath toPath:newPath error:&error]) {
//saved
NSLog(@"video saved");
} else {
//error
NSLog(@"error occured : %@", error);
}
根据文档,方法copyItemAtPath
返回BOOL
如果项目复制成功或文件管理器的委托故意停止操作,则为“是”。如果发生错误,则返回NO
因此,您可以执行以下操作:
if ([fileManager copyItemAtPath:tempFilePath toPath:newPath error:&error]) {
//saved
NSLog(@"video saved");
} else {
//error
NSLog(@"error occured : %@", error);
}