2013-04-29 169 views
0

我試圖在錄製文件後將文件夾中的影片保存到文件夾中。將記錄的視頻保存到文檔文件夾中

這是我的代碼:

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



    NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path]; 
    [self dismissViewControllerAnimated:YES completion:nil]; 
    NSLog(@"path from image picker: %@", moviePath); 

    NSFileManager* fileManager = [NSFileManager defaultManager]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); 
    NSString* documentsDir = [paths objectAtIndex:0]; 

    NSLog(@"%@",documentsDir); 

    [fileManager moveItemAtPath:moviePath toPath:documentsDir error:nil]; 

    NSArray *files = [fileManager contentsOfDirectoryAtPath:documentsDir 
                error:nil]; 

    for (id object in files) { 
     NSLog(@"%@",object); 
    } 
} 

正如你可能猜到了,這是行不通的。

第一個日誌說:從圖片選擇路徑:/private/var/mobile/Applications/6630FBD3-1212-4ED0-BC3B-0C23AEEFB267/tmp/capture-T0x1e575330.tmp.SQQ2C5/capturedvideo.MOV

秒記錄說:/ var/mobile/Applications/6630FBD3-1212-4ED0-BC3B-0C23AEEFB267/Documents

任何人都知道爲什麼它不工作?

+0

檢查我的答案,保存視頻到目錄我已更新它。 – 2013-04-29 08:12:23

回答

2

嘗試此代碼,當您保存某些內容時,該文件應具有該文件的文件名和擴展名。在您的代碼中,目標保存路徑就是文檔目錄。那是錯的。使用[documentsDir stringByAppendingPathComponent:filename.extension]會給你正確的目的地filePath,[moviePath lastPathComponent]返回所選文件的文件名+擴展名。

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

    NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path]; 
    [self dismissViewControllerAnimated:YES completion:nil]; 
    NSLog(@"path from image picker: %@", moviePath); 

    NSFileManager* fileManager = [NSFileManager defaultManager]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES); 
    NSString* documentsDir = [paths objectAtIndex:0]; 
    NSString *savePath = [documentsDir stringByAppendingPathComponent:[moviePath lastPathComponent]]; 

    NSLog(@"%@",savePath); 

    [fileManager moveItemAtPath:moviePath toPath:savePath error:nil]; 

    NSArray *files = [fileManager contentsOfDirectoryAtPath:documentsDir 
                 error:nil]; 

    for (id object in files) { 
     NSLog(@"%@",object); 
    } 
} 
+0

似乎在工作! :)請解釋... – user2328703 2013-04-29 07:42:29

+1

我在答案中增加了幾行,通過它們。告訴我,如果你想要更多的解釋,希望你將它標記爲答案 – 2013-04-29 07:43:59

相關問題