-1

I am trying to get picture taken date while choosing picture from Photo Library in iPhone. I am using below code to do the same.

NSDictionary* fileAttribs = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
NSDate *result = [fileAttribs fileCreationDate]; //or fileModificationDate

But it is showing current date and time.

Is there any way to get picture taken date while choosing picture from Photo library.

Shivomkara Chaturvedi
  • 1,697
  • 7
  • 28
  • 42
  • check this,http://stackoverflow.com/questions/7688983/ios-uiimagepickercontroller-any-way-of-getting-the-date-of-the-chosen-picture – karthika Oct 17 '13 at 09:37

2 Answers2

3

You can do it checking the metadata of the picture with ALAsset

Look for the key ALAssetPropertyDate

EDIT, complete code:

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

    NSURL *url = [info objectForKey:@"UIImagePickerControllerReferenceURL"];

    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
    [assetslibrary assetForURL:url
                   resultBlock:^(ALAsset *asset) {
                       NSDate *myDate = [asset valueForProperty:ALAssetPropertyDate];
                       NSLog(@"Date: %@", myDate);
                   } failureBlock:^(NSError *error) {
                       NSLog(@"Error");
                   }];

    [picker dismissViewControllerAnimated:YES completion:nil];

}
Odrakir
  • 4,254
  • 1
  • 20
  • 52
  • Please, check the docs, try it for yourself and if it doesn't work, come back with a specific question. – Odrakir Oct 17 '13 at 09:22
  • Okay I checked. My code is: NSURL *url=[NSURL URLWithString:fileUrl]; ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init]; //The key UIImagePickerControllerReferenceURL allows you to get an ALAsset, which then allows you to get metadata (such as the date the media was created) [lib assetForURL:url resultBlock:^(ALAsset *asset) { NSLog(@"created: %@", asset); } failureBlock:^(NSError *error) { NSLog(@"error: %@", error); }]; Output is: created: (null) – Shivomkara Chaturvedi Oct 17 '13 at 09:32
  • 1
    Get the url like this: NSURL* url = [info objectForKey:@"UIImagePickerControllerReferenceURL"]; – Odrakir Oct 17 '13 at 09:47
  • I cant do that because I am using phonegap.. I get url from phonegap plugin like below: file://localhost/Users/shivam/Library/Application%20Support/iPhone%20Simulator/6.1/Applications/3007041C-CEA0-4A8C-884D-91F8268542D4/tmp/cdv_photo_004.png – Shivomkara Chaturvedi Oct 17 '13 at 09:58
  • Then I can't help you. There must be something wrong with that url. – Odrakir Oct 17 '13 at 10:09
0

ALAssetsLibrary is deprecated, so with PHAsset:

#import <Photos/PHAsset.h> 

Then:

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

        NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
        PHAsset *phAsset = [[PHAsset fetchAssetsWithALAssetURLs:@[imageURL] options:nil] lastObject];
        NSDate *date = [phAsset creationDate];
        UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];

}
dmtr
  • 214
  • 3
  • 9