我有视频网址,想要使用此URl从iPhone中删除它

时间:2012-11-02 10:43:21

标签: iphone ios avfoundation

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

我的应用相机为我提供了如下输出网址:

file://localhost/private/var/mobile/Applications/4B2E02E5-3EE2-493E-8ECF-4B1DA29B9387/tmp/output.mov

我试过这个但是没有用:

- (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];
    }
}

3 个答案:

答案 0 :(得分:0)

你可以像这样获得Document Directory path

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"); 
}

答案 1 :(得分:0)

检查[NSHomeDirectory() stringByAppendingPathComponent:@"tmp/output.mov"];[library writeVideoAtPathToSavedPhotosAlbum:outputFileURL completionBlock:^(NSURL *assetURL, NSError *error) {

中的outputFileURL相同

答案 2 :(得分:0)

答案很简单, 您无法从文档目录中删除文件。它的可读性和可写性。

您可以通过为tmp目录提供路径来实现您的目的,然后为了保存目的将其从tmp路径移动到文档目录。而已。