How do I get the creation date and time of a AVURLAssett objective-c

444 views Asked by At

I have an app where I am picking a video from a photo library and I want to get the date and time it was created (and if possible the location it was taken) from its metadata so I can use it for some labeling. I can do it for an image picked from the photo library but it doesn't work with a video. Here is what I have for the image but I can't seem to adapt it to a video.

So I did some adapting like this:

//I get the video from the photo library

if (picker.sourceType ==UIImagePickerControllerSourceTypePhotoLibrary) {
        NSLog(@"info:%@",info);
       NSURL * movieURL = [info valueForKey:UIImagePickerControllerMediaURL] ;

 NSData * movieData = [NSData dataWithContentsOfURL:movieURL];

//I save it to the documentsDirectory and get an image at the half way point

NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [documentPaths objectAtIndex:0];
        NSString *slash = [documentsDirectory stringByAppendingPathComponent:@"/"];
        NSString *documentsDirectory2 = [slash stringByAppendingPathComponent:self.user.text];
        NSString *documentsDirectory21 = [documentsDirectory2 stringByAppendingPathComponent:@"/Movie"];
        if (![[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory21])
            [[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory21 withIntermediateDirectories:NO attributes:nil error:&error];
        NSString *fullPath = [documentsDirectory21 stringByAppendingPathComponent:[[self imageNameTextField]text]];
        fullPath = [fullPath stringByAppendingFormat:@".mp4"];
        [ movieData writeToFile:fullPath atomically:YES];
        
        
        AVURLAsset *anAsset = [[AVURLAsset alloc] initWithURL:movieURL options:nil];

//this is what I changed

    PHAsset *theAsset = [info valueForKey:UIImagePickerControllerMediaURL] ;
    
    NSLog(@"creationDate1:%@",theAsset);
    NSLog(@"creationDate2:%@",anAsset.creationDate.value);
  
    NSDate *creationDate =(NSDate *)anAsset.creationDate.value;
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MMMM-dd-yyyy"];
    dayString = [dateFormatter stringFromDate:creationDate];
    NSDateFormatter *dateFormatter2=[[NSDateFormatter alloc] init];
    [dateFormatter2 setDateFormat:@"hh:mm a"];
    timeString = [dateFormatter2 stringFromDate:creationDate];

if ([[anAsset tracksWithMediaType:AVMediaTypeVideo] count] > 0) {
            
            AVAssetImageGenerator *imageGenerator =
            [AVAssetImageGenerator assetImageGeneratorWithAsset:anAsset];
            Float64 durationSeconds = CMTimeGetSeconds([anAsset duration]);
           
            CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600);
            NSError *error;
            CMTime actualTime;
            
            CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:midpoint actualTime:&actualTime error:&error];
           
            if (halfWayImage != NULL) {
                
                NSString *actualTimeString = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, actualTime));
                NSString *requestedTimeString = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, midpoint));
                NSLog(@"Got halfWayImage: Asked for %@, got %@", requestedTimeString, actualTimeString);
                
                myImage.image = [UIImage imageWithCGImage:halfWayImage scale:1.0 orientation:UIImageOrientationRight];
                // Do something interesting with the image.
                CGImageRelease(halfWayImage);
                
            }
        }

what I had here before I discarded.

1

There are 1 answers

0
user1114881 On BEST ANSWER

So I finally figured this out. By putting the following code after

AVURLAsset *anAsset = [[AVURLAsset alloc] initWithURL:movieURL options:nil];

PHAsset *theAsset = [info valueForKey:UIImagePickerControllerMediaURL] ;

NSLog(@"creationDate1:%@",theAsset);
NSLog(@"creationDate2:%@",anAsset.creationDate.value);

NSDate *creationDate =(NSDate *)anAsset.creationDate.value;
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMMM-dd-yyyy"];
dayString = [dateFormatter stringFromDate:creationDate];
NSDateFormatter *dateFormatter2=[[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:@"hh:mm a"];
timeString = [dateFormatter2 stringFromDate:creationDate];

I've changed my original post to reflect the changes. Now if I could just get the metadata for location from the asset I'd be all set.