user3115014
user3115014

Reputation: 697

Get image location from iOS UIImagePickerController

I am try to develop iOS app which get the location of the Photo using UIImagePickerController and display it I am deploying it on iOS 7 device Here is what i did .

- (IBAction)takePhoto:(id)sender
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;
    picker.showsCameraControls = YES;

    [self presentViewController:picker animated:YES
                     completion:^ {
//                         [picker takePicture];
                     }];}




-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSLog(@"Media Info: %@", info);

    NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];



    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        // Get the location property from the asset


        CLLocation *location = [myasset valueForProperty:ALAssetPropertyLocation];
        // I found that the easiest way is to send the location to another method

        latitude =location.coordinate.latitude; //[[gpsdata valueForKey:@"Latitude"]floatValue];
        longitutde =location.coordinate.longitude;

       NSString * strLocation=[NSString stringWithFormat:@"La:%f Lo%f",latitude,longitutde];



    };
    // This block will handle errors:
    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
    {
        NSLog(@"Can not get asset - %@",[myerror localizedDescription]);
        // Do something to handle the error
    };








    if([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
        UIImage *photoTaken = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

        //Save Photo to library only if it wasnt already saved i.e. its just been taken
        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
            UIImageWriteToSavedPhotosAlbum(photoTaken, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
        }


    }

    [picker dismissModalViewControllerAnimated:YES];
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    UIAlertView *alert;
    //NSLog(@"Image:%@", image);
    if (error) {
        alert = [[UIAlertView alloc] initWithTitle:@"Error!"
                                           message:[error localizedDescription]
                                          delegate:nil
                                 cancelButtonTitle:@"OK"
                                 otherButtonTitles:nil];
        [alert show];
    }

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissModalViewControllerAnimated:YES];
}

I am not able to get .How to get the location ? please help me on this .

Upvotes: 1

Views: 1397

Answers (2)

Harshini Nerella
Harshini Nerella

Reputation: 99

Location Services Should be enabled when the photo was taken.Try same code by enabling location services.

Upvotes: 0

Hiren Varu
Hiren Varu

Reputation: 1477

in the delegate implementation you can use the path of the image or the image itself:

// This method is called when an image has been chosen from the library or taken from the camera.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//You can retrieve the actual UIImage
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
//Or you can get the image url from AssetsLibrary
    NSURL *path = [info valueForKey:UIImagePickerControllerReferenceURL];
    [picker dismissViewControllerAnimated:YES completion:^{
    }];
}

Upvotes: 2

Related Questions