getting creation date of video picked from photos library iOS

Discussion in 'iOS Programming' started by gbenna, Oct 13, 2016.

  1. gbenna, Oct 13, 2016
    Last edited by a moderator: Oct 14, 2016

    gbenna macrumors member

    Joined:
    Jul 27, 2011
    #1
    I have an app that the user can either pick a video from device photos or take a video and then save it to the apps documents directory. I want to fill in some text fields with the creation date of the video. I can fill in current time and date when the video is taken but I want to get the creation info from the video's metadata when it is picked from the photos camera roll.

    I have tried many methods with no success.
    this is one method:
    Code:
    [INDENT][INDENT]    NSURL * movieURL = [info valueForKey:UIImagePickerControllerMediaURL] ;
       ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
        [assetslibrary assetForURL:movieURL
                       resultBlock:^(ALAsset*asset) {
                           NSDate *myDate = [asset valueForProperty:ALAssetPropertyDate];
                           NSLog(@"Date: %@", myDate);
    
                       } failureBlock:^(NSError *error) {
                           NSLog(@"Error");
                       }];[/INDENT][/INDENT]
    
    this is another
    
    [INDENT][INDENT]    CGImageSourceRef source = CGImageSourceCreateWithURL( (CFURLRef)movieURL, NULL);
    
         NSDictionary* metadata = (NSDictionary   *)CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source,0,NULL));
           NSLog(@"image data %@", metadata);[/INDENT][/INDENT]
    
    this is another
    //this is inside picker finished picking method
    
        
    [INDENT][INDENT][INDENT][self metadata:movieURL];
    
    -(void)metadata:(NSURL *)url
    
    {
    
      
    
        PHAsset *asset=[PHAsset fetchAssetsWithALAssetURLs:mad: options:nil].firstObject; if (asset) { // get photo info from this asset PHImageRequestOptions * imageRequestOptions = [[PHImageRequestOptions alloc] init]; imageRequestOptions.synchronous = YES; [[PHImageManager defaultManager] requestImageDataForAsset:asset options:imageRequestOptions resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) { [self metadataFromImageData:imageData];// as this imageData is in NSData format so we need a method to convert this NSData into NSDictionary to display metadata }]; } else { NSLog(@"no asset"); } } -(NSDictionary*)metadataFromImageData:(NSData*)imageData{ CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)(imageData), NULL); if (imageSource) { NSDictionary *options = @{(NSString *)kCGImageSourceShouldCache : [NSNumber numberWithBool:NO]}; CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, (__bridge CFDictionaryRef)options); if (imageProperties) { NSDictionary *metadata = (__bridge NSDictionary *)imageProperties; CFRelease(imageProperties); CFRelease(imageSource); NSLog(@"Metadata of selected image%@",metadata);// It will display the metadata of image after converting NSData into NSDictionary return metadata; } CFRelease(imageSource); } NSLog(@"Can't read metadata"); return nil; }[/INDENT][/INDENT][/INDENT] nothing seems to work My movieURL log is [INDENT][INDENT][INDENT]2016-10-13 09:58:51.045271 my little world app[2641:1388034] movieURL;file:///private/var/mobile/Containers/Data/Application/22A178E5-74C4- 48EF-B487- 5E01321508AD/tmp/trim.04F0AC8A-2960-464D-8670-7E79662EAB9B.MOV I would really appreciate some help with this as I have spent hrs and hrs trying to figure this out and there seems to be nothing on the internet[/INDENT][/INDENT][/INDENT]
    [/INDENT][/INDENT][/INDENT]
     
  2. gbenna, Oct 27, 2016
    Last edited by a moderator: Oct 28, 2016

    gbenna thread starter macrumors member

    Joined:
    Jul 27, 2011
    #2
    I figured this out. Here is my code
    Code:
        if (picker.sourceType ==UIImagePickerControllerSourceTypePhotoLibrary) {
    
        NSURL * movieURL = [info valueForKey:UIImagePickerControllerMediaURL] ;
    
          [self getMediaName:nil url:[info objectForKey:UIImagePickerControllerReferenceURL]];
         }
    
    
        - (void)getMediaName:(UIImage*)originalImage url:(NSURL*)url {
         @try {
            ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *asset) {
                if (asset == nil) return;
                ALAssetRepresentation *assetRep = [asset defaultRepresentation]; 
    
    //I get the asset location
    
                CLLocation *location = [asset valueForProperty:ALAssetPropertyLocation];
    
     //I break that into the component parts
    
            CLGeocoder *ceo = [[CLGeocoder alloc]init];
               CLLocation *loc = [[CLLocation alloc]initWithLatitude:location.coordinate.latitude     longitude:location.coordinate.longitude]; //insert your coordinates
    
    //then I reverse location to get a street address and Zip Code and make a string from those
    
                  [ceo reverseGeocodeLocation:loc 
                         completionHandler:^(NSArray *placemarks, NSError *error) {
                              CLPlacemark *placemark = [placemarks objectAtIndex:0];
                              if (placemark) {
                                  NSLog(@"placemark %@",placemark);
                                 NSString *strString =placemark.name;
                                  NSString *midstring = [strString stringByAppendingString:mad:","];
                                  if (placemark.postalCode == nil) {
                                      postalcodeString = @"00000";
                                  }
                                  else{
                                      postalcodeString = placemark.postalCode;
                                  }
    
                                  combineString = [midstring stringByAppendingString:postalcodeString];
                                  NSLog(@"combineString:%@",combineString);
    
    //Then I access the user's address book and look for the street address and zip codes of each person and put those together into a string.
    
                                  CFErrorRef *error = NULL;
                                 ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
                                  //ABAddressBookRef addressBook = ABAddressBookCreate();
                                  CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
                                  CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);
    
                                  for(int i = 0; i < numberOfPeople; i++) {
                                      NSLog(@"i;%d",i);
    
                                      ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );
    
    
                                      NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person,  kABPersonFirstNameProperty));
    
                                      NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person,  kABPersonLastNameProperty));
                                      NSLog(@"Name:%@ %@", firstName, lastName);
    
                                      ABMutableMultiValueRef address = (ABRecordCopyValue(person, kABPersonAddressProperty));
    
                                      if(ABMultiValueGetCount(address) > 0) {
                                          CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(address, 0);
                                          NSString *streetString = CFDictionaryGetValue(dict, kABPersonAddressStreetKey);
                                          NSString *zipString = CFDictionaryGetValue(dict, kABPersonAddressZIPKey);
                                          if (zipString == Nil) {
                                              zipString = @"00000";
                                          }
                                          NSString *interstring = [streetString stringByAppendingString:mad:","];
                                          NSString *allString = [interstring stringByAppendingString:zipString];
                                          NSLog(@"allstring;%@ %@",allString, combineString);
    
    
    //Then I compare the location string to the address string. If there is a match I save the name of the person of that address and attach it to the address later to fill in a textview.
    
                                          if ([combineString isEqualToString:allString]) {
                                             NSLog(@"its a match");
                                              NSLog(@"i:%d",i);
                                              firstNameString = firstName;
    
                                          }
    
                                          else {
    
                                          }
    
    
                                          NSLog(@"streetString:%@",streetString);
                                          NSLog(@"firstNameString:%@",firstNameString);
                                          NSLog(@"combineString:%@",combineString);
                                                                          }
                                  }}
                              else {
                                  NSLog(@"Could not locate");
                              }
                          }
                       ];
    
    // Do what you need with the file name here
                  };
    
                 ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *error) {
    
                 };
    
                   ALAssetsLibrary *library = [ALAssetsLibrary new];
                  [library assetForURL:url resultBlock:resultblock failureBlock:failureblock];
                }
                [USER=40907]@Catch[/USER] (NSException *exception) {
    
                  }
    
    Thanks for everyone's help.
     

Share This Page