【问题标题】:UIImagePickerController doesn't delete temp video capture file from /tmp/captureUIImagePickerController 不会从 /tmp/capture 中删除临时视频捕获文件
【发布时间】:2013-12-12 02:02:24
【问题描述】:

我的应用正在使用 UIImagePickerController 录制视频并将其保存到相机胶卷中,但我的问题是录制的视频也保存在 /tmp/capture 目录中并且不会被删除。由于这个原因,我的应用程序使用大量不必要的数据慢慢积累了存储在 tmp/capture 目录中的一长串视频。我正在使用 ARC 和 iOS7 SDK。

-(BOOL)startCameraControllerFromViewController:(UIViewController*)controller       usingDelegate:(id )delegate forType: (int)type 
{ 
    if (([UIImagePickerController     isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO)
    || (delegate == nil)
    || (controller == nil)) {
    return NO;
    }

    UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
    cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;

    if(type == IMAGE )
        cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeImage, nil];
     if(type == VIDEO)
     {
         cameraUI.videoMaximumDuration = 60.0f;
         cameraUI.videoQuality = UIImagePickerControllerQualityType640x480; 
         cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
     }
     cameraUI.allowsEditing = NO;
     cameraUI.delegate = delegate;
     [controller presentViewController: cameraUI animated: YES completion:nil];
     return YES;
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

 {   
     NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];

     if(CFStringCompare ((__bridge_retained CFStringRef)mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo)//video
     {
         NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];

         if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath)) 
             UISaveVideoAtPathToSavedPhotosAlbum(moviePath,   self,@selector(video:didFinishSavingWithError:contextInfo:), nil);

         [self dismissViewControllerAnimated:NO completion:nil];
         globalDel.videoPath = moviePath;
         globalDel.videoToUpload = [NSData dataWithContentsOfFile:moviePath];
     }
}

globalDel.videoPath 定义为:@property(nonatomic,retain)NSString *videoPath; globalDel.videoToUpload 定义为:@property(nonatomic,retain)NSData *videoToUpload; 完成后我将它们设置为 NULL。

我使用委托文件来保存这些引用以供不同导航控制器之间使用。

为什么应用程序每次都将文件保存到 tmp 文件夹,我该如何阻止它这样做?

谢谢

【问题讨论】:

    标签: ios iphone memory uiimagepickercontroller


    【解决方案1】:

    上传完成后,您需要删除包含电影的目录(这也会删除临时文件):

    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:globalDel.videoPath]) {
        NSError *error;
        // Attempt to delete the folder containing globalDel.videoPath
        if ([fileManager removeItemAtPath:[globalDel.videoPath stringByDeletingLastPathComponent] error:&error] != YES) {
            NSLog(@"Unable to delete file: %@", [error localizedDescription]);
        }
    }
    

    【讨论】:

    • iOS 会保留这个临时文件多久?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多