替换UISaveVideoAtPathToSavedPhotosAlbum @selector videoSaved函数

时间:2016-06-17 06:35:19

标签: ios objective-c

  • Xcode 7
  • Swift 2
  • 目标C

我已经更改了一些Objective C代码,我根本不知道Objective C语法。我已将以下将视频保存到相机胶卷的代码替换为:

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相机胶卷的Objective C代码中,有一个Objective 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函数是UISaveVideoAtPathToSavedPhotosAlbum的特殊签名,但我对Objective C不熟悉,我不知道如何编写一个我可以调用的新函数,并通过我的错误对象并维护videoSaved的实现。

1 个答案:

答案 0 :(得分:0)

根据NSFileManager文档,方法copyItemAtPath返回BOOL。

  

如果项目已成功复制或文件管理器的委托故意停止操作,则为“是”。如果发生错误,则返回NO。

所以你可以这样做:

if ([fileManager copyItemAtPath:tempFilePath toPath:newPath error:&error]) {
      //saved
      NSLog(@"video saved");
} else {
      //error
      NSLog(@"error occured : %@", error);
}