2011-09-20 20 views
1

我正在使用uiimagepicker將視頻保存到我的應用程序包中。但保存之後,視頻的創建日期會發生變化。我的要求只是將該視頻複製到我的文件夾而不更改其創建的日期。使用uiimagepickercontroller保存視頻而無需更改其創建日期

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    [[picker parentViewController] dismissModalViewControllerAnimated:YES]; 

    NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL]; 
    NSData *imageData = [NSData dataWithContentsOfURL:url]; 
    NSString *path = NSHomeDirectory(); 
    NSString *img_temp_name=[[NSString stringWithFormat:@"test"] stringByAppendingString:@".mov"]; 
    NSString *full_path=[path stringByAppendingPathComponent:img_temp_name]; 

    if([imageData writeToFile:full_path atomically:YES]) { 
    } 
+0

後一些代碼。 – Gypsa

回答

3

你可以使用NSFileManager來幫助你嗎?

我認爲你不會被允許只是移動電影,但如果你複製它,它是否保留一些文件信息?

NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL]; 
NSString *path = NSHomeDirectory(); 
NSString *img_temp_name=[[NSString stringWithFormat:@"test"] stringByAppendingString:@".mov"]; 
NSString *full_path=[path stringByAppendingPathComponent:img_temp_name]; 

NSURL *dst = [NSURL fileURLWithString:full_path]; 

NSError *error = nil; 
NSFileManager *manager = [[[NSFileManager alloc] init] autorelease]; 
BOOL success = [manager copyItemAtURL:url toURL:dst error:&error]; 

if (NO == success || error) { 
    NSLog(@"Could not copy : %@", error); 
} 
+0

不工作.... 我做喜歡 更改我的代碼 - (空)imagePickerController:(*的UIImagePickerController)選擇器didFinishPickingMediaWithInfo:(NSDictionary的*)信息{ NSURL * URL = [信息objectForKey:UIImagePickerControllerReferenceURL]。 NSString * path = NSHomeDirectory(); NSURL * dst = [NSURL fileURLWithPath:path]; NSError * error = nil; NSFileManager * manager = [[[NSFileManager alloc] init] autorelease]; BOOL成功= [manager copyItemAtURL:url toURL:dst error:&error];如果(NO == success || error){ \t \t NSLog(@「Could not copy:%@」,error); \t} } – pop924

+0

日誌無法複製:錯誤域= NSCocoaErrorDomain代碼= 262「操作無法完成(可可錯誤262)」 – pop924

+0

當您執行操作時,您的url和dst值是什麼?複製? – deanWombourne

0

你的問題是,作爲挑選過程的一部分,視頻被重新編碼。這改變了創建日期。我也想知道是否有可能獲得這些信息。

0

這是一種將視頻作爲NSData獲取的方法。 它使用照片框架ALAssetLibrary被棄用iOS9的:

IMPORTANT

The Assets Library framework is deprecated as of iOS 9.0. Instead, use the Photos framework instead, which in iOS 8.0 and later provides more features and better performance for working with a user’s photo library. For more information, see Photos Framework Reference.

import Photos 

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 
    self.dismissViewControllerAnimated(true, completion: nil) 

    if let referenceURL = info[UIImagePickerControllerReferenceURL] as? NSURL { 
     let fetchResult = PHAsset.fetchAssetsWithALAssetURLs([referenceURL], options: nil) 
     if let phAsset = fetchResult.firstObject as? PHAsset { 
      PHImageManager.defaultManager().requestAVAssetForVideo(phAsset, options: PHVideoRequestOptions(), resultHandler: { (asset, audioMix, info) -> Void in 
       if let asset = asset as? AVURLAsset { 
        let videoData = NSData(contentsOfURL: asset.URL) 

        // optionally, write the video to the temp directory 
        let videoPath = NSTemporaryDirectory() + "tmpMovie.MOV" 
        let videoURL = NSURL(fileURLWithPath: videoPath) 
        let writeResult = videoData?.writeToURL(videoURL, atomically: true) 

        if let writeResult = writeResult where writeResult { 
         print("success") 
        } 
        else { 
         print("failure") 
        } 
       } 
      }) 
     } 
    } 
} 
相關問題