Search code examples
objective-cxcodeios7alassetslibraryalasset

getting ALAssets from photos library iOS


I am trying to get the time, date and location from a movie i took on my iPhone/iPad This is the code i used

//i get the video picked and then save it to app but i think i am getting the movie's asset data before i save it.

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{   NSError *error;
 else if (picker.sourceType ==UIImagePickerControllerSourceTypePhotoLibrary) {

    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");
                   }];
    NSData * movieData = [NSData dataWithContentsOfURL:movieURL];
    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];
} 

however myDate is logged as null

what am i doing wrong

I just tried this code also (added 10/11)

CGImageSourceRef source = CGImageSourceCreateWithURL( (CFURLRef)movieURL, NULL);

    NSDictionary* metadata = (NSDictionary *)CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source,0,NULL));
    NSLog(@"image data %@", metadata);

but still metadata is null;

I read that videos don't have the same metadata as images. if this is true then how do I access the video metadata since I can see data tied to the video in photos.

any help would be greatly appreciated.

I have tried everything I could find on the internet to help me do this but nothing seems to work. When I log my movieURL this is what I get

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

So since it is in a temporary file can I not get the metadata.

Does the Metadata exits in the movies NSData and can I get it from that?

I am pulling my hair out.


Solution

  • So I figured this out here is the code

    - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    
    {
    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];
                NSString *fileName = [assetRep filename];
     NSDate *myDate = [asset valueForProperty:ALAssetPropertyDate];
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
                [dateFormatter setDateFormat:@"MM-dd-yyyy"];
                NSLog(@"%@",[dateFormatter stringFromDate:myDate]);
    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *error) {
    
            };
    
            ALAssetsLibrary *library = [ALAssetsLibrary new];
            [library assetForURL:url resultBlock:resultblock failureBlock:failureblock];
        }
        @catch (NSException *exception) {
    
        }
    }
    

    you can use any valueForProperty