从iOS UIImagePickerController获取图像位置

时间:2014-09-26 07:42:09

标签: ios location uiimagepickercontroller

我正在尝试开发iOS应用程序,使用UIImagePickerController获取Photo的位置并显示它我在iOS 7设备上部署它这就是我做的。

- (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];
}

我无法得到。如何获得位置?请帮助我。

2 个答案:

答案 0 :(得分:2)

在委托实现中,您可以使用图像的路径或图像本身:

// 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:^{
    }];
}

答案 1 :(得分:0)

位置服务应该在拍摄照片时启用。通过启用位置服务来尝试相同的代码。