Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

objective c - iPhone Get UIImagePickerController Lat/Lng

I'm using the UIImagePickerController to take a photo and then upload the photo to a WCF Service. Along with the image, I also need to send to the latitude and longitude of the photo.

How can I get hold of this information from the photo taken?

I've searched online but can't seem to find a good source of info. I've seen about the EXIF data that is passed through the UIImagePickerControllerMediaMetadata key as part of the didFinishPickingMediaWithInfo delegate method. However, when I print out the contents to the log, there's no location information.

Am I missing something, do I need to turn on location services prior to taking the photo? Or is there another way to get the information?

Many thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I've been figuring this out for a couple of days, and finally got a solution. As already suggested, you can use ALAssetsLibrary.

The image picker will give you a dictionary, which contains an url that points to the asset.

The ALAssetsLibrary's assetForURL: resultBlock: failureBlock: method uses blocks. You can read more about them from example here. So this is how we handle the info given by the picker:

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

    if ([picker sourceType] == UIImagePickerControllerSourceTypePhotoLibrary) {
        // We'll store the info to use in another function later
        self.imageInfo = info;

        // Get the asset url
        NSURL *url = [info objectForKey:@"UIImagePickerControllerReferenceURL"];

        // We need to use blocks. This block will handle the ALAsset that's returned: 
        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
            [self handleImageLocation:location];
        };
        // This block will handle errors:
        ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
        {
            NSLog(@"Can not get asset - %@",[myerror localizedDescription]);
            // Do something to handle the error
        };


        // Use the url to get the asset from ALAssetsLibrary,
        // the blocks that we just created will handle results
        ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
        [assetslibrary assetForURL:url 
                       resultBlock:resultblock
                      failureBlock:failureblock];

    }
    [picker dismissModalViewControllerAnimated:YES];
    [picker release];
}

And next the method that handles the image and location data:

- handleImageLocation:(CLLocation *)location 
{
    UIImage *image = [self.imageInfo objectForKey:UIImagePickerControllerOriginalImage];
    // Do something with the image and location data...
}

And of course you can also get other information about the image with this method by using keys:

ALAssetPropertyType
ALAssetPropertyLocation
ALAssetPropertyDuration
ALAssetPropertyOrientation
ALAssetPropertyDate
ALAssetPropertyRepresentations
ALAssetPropertyURLs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...