pop924
pop924

Reputation: 171

Save videos using uiimagepickercontroller without changings its created date

I am using uiimagepicker to save video to my application bundle. But after saving that, the created date of the video is changed. My requirement is just to copy that video to my folder without changing its created date.

- (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]) {
    } 

Upvotes: 1

Views: 1904

Answers (3)

Alonzo
Alonzo

Reputation: 783

Here is a way to get videos as NSData. It uses the Photos framework as ALAssetLibrary is deprecated as of 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")
                    }
                }
            })
        }
    }
}

Upvotes: 0

box86rowh
box86rowh

Reputation: 3415

Your issue is that as part of the picking process the video is reencoded. This changes the creation date. I would also like to know if it is possible to getbthis info.

Upvotes: 0

deanWombourne
deanWombourne

Reputation: 38475

Can you use NSFileManager to help you?

I assume that you're not going to be allowed just to move the movie but if you copy it, does it preserve some file information?

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

Upvotes: 3

Related Questions