1

i have an app where a user can either take a photo or select one from their camera roll. I need to be able to extract the Location (lat & long) from where the image was taken. Currently i am using the location manager to get the location of the device, but this could lead to inaccuracies in location (i.e. someone may take the photo but not email it until they get home, thus changing the location)

i found a solution here which uses "asset libraries", but I'm getting a SIGBRAT error when i select a photo from my camera roll.

Could someone please either show me where I'm going wrong in my code, or suggest an easy way of getting the location the photo was taken.

the following is my code:

- (IBAction)takePhoto
{
    picker1 = [[UIImagePickerController alloc] init];
    picker1.delegate = self;
    [picker1 setSourceType: UIImagePickerControllerSourceTypeCamera];
    [self presentViewController:picker1 animated:YES completion:NULL];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];


}

- (IBAction)chooseExisting
{
    picker2 = [[UIImagePickerController alloc] init];
    picker2.delegate = self;
    [picker2 setSourceType: UIImagePickerControllerSourceTypePhotoLibrary];
    [self presentViewController:picker2 animated:YES completion:NULL];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];



}


#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        longditute = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
        Latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    }
    // Stop Location Manager
    [locationManager stopUpdatingLocation];
}



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

   image = [info objectForKey:UIImagePickerControllerOriginalImage];
   [imageView setImage:image];
   [self dismissViewControllerAnimated:YES completion:NULL];

    assetsLibrary = [[ALAssetsLibrary alloc] init];
    groups = [NSMutableArray array];

    [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos  usingBlock:^(ALAssetsGroup *group, BOOL *stop)
    {
        if (group == nil)
        {
            return;
        }

        [groups addObject:group];

    } failureBlock:^(NSError *error)
    {
        // Possibly, Location Services are disabled for your application or system-wide. You should notify user to turn Location Services on. With Location Services disabled you can't access media library for security reasons.

    }];
    ALAssetsGroup *group = [groups objectAtIndex:0];
    [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
     {
         if (result == nil)
         {
             return;
         }

         // Trying to retreive location data from image
         CLLocation *loc = [result valueForProperty:ALAssetPropertyLocation];
         NSLog(loc);
     }];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:NULL];
}
scb998
  • 899
  • 5
  • 18
  • 42
  • On what line are you getting the `SIGABRT`? What does the log say? What does the stack trace say? – Tommy Jan 21 '14 at 02:40

1 Answers1

0

You said, OR can you show an easy way of grabbing the data.

So I can point you onto two posts that deal with this exact matter. You first need to make sure that the user has given the app permissions to allow location services to grab the required details for the app.

Nice answer given over herethat covers the point addressed up including code and another great answer given here in more detail Check the first one out first and then the second link.

Community
  • 1
  • 1
Pavan
  • 17,840
  • 8
  • 59
  • 100
  • I'm getting a declaration error at the following line: `CGImageDestinationRef dr = CGImageDestinationCreateWithURL ((__bridge CFURLRef)fileURL, ` – scb998 Jan 21 '14 at 03:10
  • 1
    I dont know about that, its best you comment on the person who posted the answer in the links I gave you. The purpose of my answer was to push you in the right direction which I've done. – Pavan Jan 21 '14 at 03:31