我有视频URL,希望使用此URL将其从iPhone中删除

我有视频URL,希望使用此URL将其从iPhone中删除,iphone,ios,avfoundation,Iphone,Ios,Avfoundation,我已经创建了一个存储视频的应用程序,现在我想在用户单击删除按钮时删除该视频。我正在使用AVFoundation录制视频 我的app camera为我提供如下输出URL: file://localhost/private/var/mobile/Applications/4B2E02E5-3EE2-493E-8ECF-4B1DA29B9387/tmp/output.mov NSString *strPath = [NSHomeDirectory() stringByAppendingPathCo

我已经创建了一个存储视频的应用程序,现在我想在用户单击删除按钮时删除该视频。我正在使用
AVFoundation
录制视频

我的app camera为我提供如下输出URL:

file://localhost/private/var/mobile/Applications/4B2E02E5-3EE2-493E-8ECF-4B1DA29B9387/tmp/output.mov
NSString *strPath =  [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSError *error;
if ([fileManager fileExistsAtPath:path]) {
  if ([[NSFileManager defaultManager] isDeletableFileAtPath:strPath]) {
    BOOL success = [[NSFileManager defaultManager] removeItemAtPath:strPath error:&error];
    if (!success) {
        NSLog(@"Error removing file at path: %@", error.localizedDescription);
    }
    else
    {
        NSLog(@"File removed  at path: %@", error.localizedDescription);
    }
  }
  else
{
    NSLog(@"file not exists"); 
}
我尝试过这个方法,但不起作用:

- (void) removeFile:(NSURL *)fileURL
{

    NSString *filePath = [fileURL path];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:filePath]) {
        NSLog(@"here it is : %@",fileURL);
        NSError *error;
        // Attempt to delete the file at filePath2
        if ([fileManager removeItemAtPath:filePath error:&error] != YES)
            NSLog(@"Unable to delete file: %@", [error localizedDescription]);

        // Show contents of Documents directory
        NSLog(@"Documents directory: %@",
              [fileManager contentsOfDirectoryAtPath:filePath error:&error]);
    }
}
编辑

   - (IBAction)videoWillDelete:(id)sender {

        NSString *strPath =  [NSHomeDirectory() stringByAppendingPathComponent:@"output.mov"];

        NSLog(@"file is:%@",strPath);

        if ([[NSFileManager defaultManager] isDeletableFileAtPath:strPath]==YES) {
            NSLog(@"file is deletebale");
        }else {
            NSLog(@"file is not deletebale");
        }

        NSError *error;
        if ([[NSFileManager defaultManager] isDeletableFileAtPath:strPath]) {

            BOOL success = [[NSFileManager defaultManager] removeItemAtPath:strPath error:&error];

            if (!success) {
                NSLog(@"Error removing file at path: %@", error.localizedDescription);
            }
        }

    }
我的日志文件有:

2012-11-02 17:33:21.836 AVFoundationCam[1791:707] file is:/var/mobile/Applications/4B2E02E5-3EE2-493E-8ECF-4B1DA29B9387/output.mov

2012-11-02 17:33:21.838 AVFoundationCam[1791:707] file is not deletebale
这个函数是我用来在iPhone中存储数据的:

else {  
        ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
        [library writeVideoAtPathToSavedPhotosAlbum:outputFileURL
                                    completionBlock:^(NSURL *assetURL, NSError *error) {

                                        if (error) {
                                            if ([[self delegate] respondsToSelector:@selector(captureManager:didFailWithError:)]) {
                                                [[self delegate] captureManager:self didFailWithError:error];
                                            }                                           
                                        }

                                        if ([[UIDevice currentDevice] isMultitaskingSupported]) {
                                            [[UIApplication sharedApplication] endBackgroundTask:[self backgroundRecordingID]];
                                        }

                                        if ([[self delegate] respondsToSelector:@selector(captureManagerRecordingFinished:)]) {
                                            [[self delegate] captureManagerRecordingFinished:self];
                                        }
                                    }];
        //[library release];
    }
}

您可以获得
文档
目录
路径
如下:

file://localhost/private/var/mobile/Applications/4B2E02E5-3EE2-493E-8ECF-4B1DA29B9387/tmp/output.mov
NSString *strPath =  [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSError *error;
if ([fileManager fileExistsAtPath:path]) {
  if ([[NSFileManager defaultManager] isDeletableFileAtPath:strPath]) {
    BOOL success = [[NSFileManager defaultManager] removeItemAtPath:strPath error:&error];
    if (!success) {
        NSLog(@"Error removing file at path: %@", error.localizedDescription);
    }
    else
    {
        NSLog(@"File removed  at path: %@", error.localizedDescription);
    }
  }
  else
{
    NSLog(@"file not exists"); 
}
现在删除如下项:

file://localhost/private/var/mobile/Applications/4B2E02E5-3EE2-493E-8ECF-4B1DA29B9387/tmp/output.mov
NSString *strPath =  [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSError *error;
if ([fileManager fileExistsAtPath:path]) {
  if ([[NSFileManager defaultManager] isDeletableFileAtPath:strPath]) {
    BOOL success = [[NSFileManager defaultManager] removeItemAtPath:strPath error:&error];
    if (!success) {
        NSLog(@"Error removing file at path: %@", error.localizedDescription);
    }
    else
    {
        NSLog(@"File removed  at path: %@", error.localizedDescription);
    }
  }
  else
{
    NSLog(@"file not exists"); 
}

检查
[NSHomeDirectory()stringByAppendingPathComponent:@“tmp/output.mov”]
[library writeVideoAtPathToSavedPhotosAlbum:outputFileURL]中的outputFileURL相同
completionBlock:^(NSURL*assetURL,NSError*error){

答案很简单, 无法从文档目录中删除文件。该文件仅可读写


您可以通过为tmp目录指定路径来实现您的目的,然后为了保存目的,将其从tmp路径移动到文档目录。就是这样。

由于您没有添加任何相关信息,这是否意味着
removitematpath
返回
YES
,并且
错误
nil
?您可以选择哪种类型的错误日志您将日志?和文档目录放入其中,并为其提供nullmaybe?:NSString*strPath=[NSHomeDirectory()stringByAppendingPathComponent:@“tmp/output.mov”];我是否必须更改stringByAppendingPathComponent:@“Documents”的值,然后再次像这样添加您的文件名:[strPath stringByAppendingPathComponent:FileName here];它说文件不可删除为什么?