UIImagePickerController doesn't delete temp vi

2019-08-11 13:35发布

问题:

My app is recording videos using a UIImagePickerController and saves it to the camera roll just fine but my problem is that the recorded video is also being saved in the /tmp/capture directory and doesn't get deleted. My app slowly accumulates a long list of videos stored in the tmp/capture directory using lots of unnecessary data because of this. I am using ARC and 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 is defined as: @property(nonatomic,retain)NSString *videoPath; globalDel.videoToUpload is defined as: @property(nonatomic,retain)NSData *videoToUpload; I set them to NULL once done with them.

I use a delegate file to keep those references for use between different navigation controllers.

Why is the app saving the file to the tmp folder each time and how do I stop it from doing that?

Thanks

回答1:

You need to delete the directory containing the movie once your upload is finished (which will also delete the temp file):

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