add GPS to video in savedPhotoAlbum

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

  1. gbenna, Oct 27, 2016
    Last edited: Oct 27, 2016

    gbenna macrumors member

    Joined:
    Jul 27, 2011
    #1
    When I take a video using UIImagePickerController and save it to SavedPhotosAlbum using

    Code:
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
           [library writeVideoAtPathToSavedPhotosAlbum:(NSURL *)movieURL
            completionBlock:^(NSURL *assetURL, NSError *error){
            if (error) {
            NSLog(@"Save video fail:%@",error);
            } else {
            NSLog(@"Save video succeed.");
             [self getsavedvideo];
             }}];
    the saved file contains a name and creation date but not the GPS info. I am surprised by this since I am using the built in camera and the imagePicker. Why wouldn't Apple just include the GPS coordinates (even if I had to ask users permission to use location services).

    anyway I digress

    I have not found anyway to add the GPS coordinates to the video as it is being saved and I haven't been able to find anyway to add the this information in the completionBlock.

    If someone knows how please let me know.

    So I wrote a bit of code that is called after the video is saved [self getsavedvideo]; above which gets the saved video and I can get the movie name and creation date and it shows there is no location info.

    Code:
    -(void) getsavedvideo;{
           ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
          // Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
           [library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group,     BOOL *stop) {
           // Within the group enumeration block, filter to enumerate just photos.
          [group setAssetsFilter:[ALAssetsFilter allVideos]];
            // Chooses the photo at the last index
            [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *alAsset,   NSUInteger index, BOOL *innerStop) {
             // The end of the enumeration is signaled by asset == nil.
              if (alAsset) {
             ALAssetRepresentation *representation = [alAsset defaultRepresentation];
             // Stop the enumerations
             *stop = YES; *innerStop = YES;
              // Do something interesting with the AV asset.
              NSString *fileName = [representation filename];
               NSDate *myDate = [alAsset valueForProperty:ALAssetPropertyDate];
               CLLocation *location = [alAsset valueForProperty:ALAssetPropertyLocation];
               NSLog(@"fileName!!!!,%@",fileName);
               NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
              [dateFormatter setDateFormat:mad:"dd-MM-yyyy 'at' HH:mm"];
               NSLog( @"date for this file is %@ %@", [[[alAsset defaultRepresentation] url] absoluteString],   [dateFormatter stringFromDate: myDate] );
                NSLog (@"locate!!!!%@",location);
                 } }];
                } failureBlock: ^(NSError *error) {
                 NSLog(@"No groups");}];}
    so does anyone know how to append this with the GPS information and save it back to the video's metadata.
    I have been wracking my brains out on this for a few days and haven't really found anything helpful.

    Plz help
     
  2. gbenna thread starter macrumors member

    Joined:
    Jul 27, 2011
    #2
    So I figured this out for a device running ios8 or newer anyway.

    add this code in the

    Code:
    -(void) getsavedvideo that was in the completionBlock above.
    
    Make sure you have 
    #import <Photos/Photos.h>
    
        if ([PHAsset class]) { // If this class is available, we're running iOS 8
               
                PHFetchOptions *fetchOptions = [PHFetchOptions new];
                fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
                fetchOptions.fetchLimit = 1;
                PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:fetchOptions];
                PHAsset *lastImageAsset = [fetchResult lastObject];
               
                [[PHImageManager defaultManager]requestImageForAsset:lastImageAsset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeDefault options:nil resultHandler:^(UIImage *result, NSDictionary *info){
                    if ([info objectForKey:PHImageErrorKey] == nil && ![[info objectForKey:PHImageResultIsDegradedKey] boolValue]) {
                       
                        NSArray *resources = [PHAssetResource assetResourcesForAsset:lastImageAsset];
                        NSString *orgFilename = ((PHAssetResource*)resources[0]).originalFilename;
                       
                       
                       
                        [lastImageAsset requestContentEditingInputWithOptions:nil
                                                   completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
                                                       NSURL *imageURL = contentEditingInput.fullSizeImageURL;
                                                       NSString *urlstring = [imageURL absoluteString];
                                                       NSLog(@"urlstring%@",urlstring);
                                                   }];
                   
                   
                       
                        CLLocationCoordinate2D    locationNew = CLLocationCoordinate2DMake( currentLocation.coordinate.latitude, currentLocation.coordinate.longitude) ;
                        NSDate *nowDate = [NSDate date];
                       
                        CLLocation *myLocation = [[CLLocation alloc ]initWithCoordinate:locationNew altitude:0.0 horizontalAccuracy:1.0 verticalAccuracy:1.0 timestamp:nowDate];
                       
                     
                        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                            // Create a change request from the asset to be modified.
                            PHAssetChangeRequest *request = [PHAssetChangeRequest changeRequestForAsset:lastImageAsset];
                            // Set a property of the request to change the asset itself.
                            request.location = myLocation;
                           
                        } completionHandler:^(BOOL success, NSError *error) {
                            NSLog(@"Finished updating asset. %@", (success ? @"Success." : error));
                        }];
                    }
               }];
            }
       
    
            else {
                  //if you aren't running ios8 or newer I haven't found a way to add/change metadata but I will keep looking.So if you leave this blank no new metadata is added.
        }
    You can add other metadata just create the data in the form needed and then use request.(what you want to change) = new data.

    you can check to see if the data already exist and if there is a metadata for it by putting in something like

    lastImageAsset (which was the PHAsset fetch result above) and then the property like

    NSDate *originalFileDate = [lastImageAsset creationDate]; etc.
    there is a list at
    https://developer.apple.com/reference/photos/phasset
     

Share This Page