ios - 如何在 UIImagePickerController 捕获图像的瞬间获取当前位置?

标签 ios objective-c image swift geolocation

我研究了如何从 UIImagePickerController 相机返回的图像中获取位置数据。但是,我认为最简单的方法是在 UIImagePickerController 捕获图像的瞬间从 CLLocationManager 获取当前位置。

有没有办法做到这一点?例如,有没有办法监听“capturePhoto”事件?

澄清一下,使用我的应用的用户可能会很快移动。

最佳答案

这是我的建议,这样您就不必再跟踪用户的位置,这样您就可以获得最接近图像实际拍摄时间的用户位置。

在你的 viewDidLoad 中实例化 CLLocationManager 类变量,例如:

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;

并确保它是授权的:

if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {
    [self.locationManager requestWhenInUseAuthorization];
}

(还在 .plist 中包含“NSLocationWhenInUseUsageDescription”键)

然后您可以等到 UIImagePickerController 实际出现,然后再 (1) 初始化字典以保存位置和 (2) 开始更新位置,例如:

[self presentViewController:self.imagePicker animated:YES completion:nil];
self.locationDictionary = [[NSMutableDictionary alloc] init];
[self.locationManager startUpdatingLocation];

此时,当返回 CLLocation 值时,您可以开始将用户的更新位置存储在 NSMutableDictionary self.locationDictionary 类实例变量中来自 didUpdateToLocation 委托(delegate)方法,例如:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    // Format the current date time to match the format of
    // the photo's metadata timestamp string
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY:MM:dd HH:mm:ss"];
    NSString *stringFromDate = [formatter stringFromDate:[NSDate date]];

    // Add the location as a value in the location NSMutableDictionary
    // while using the formatted current datetime as its key
    [self.locationDictionary setValue:newLocation forKey:stringFromDate];
}

然后一旦选择了图像,就在元数据中找到它的时间戳,并在位置字典中找到时间戳键最接近图像时间戳的值,例如:

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

    [self.locationManager stopUpdatingLocation];

    // When a photo is selected save it as a UIImage
    self.selectedPhoto = info[UIImagePickerControllerOriginalImage];

    // Get the timestamp from the metadata and store it as an NSString
    self.selectedPhotoDateTime = [[[info valueForKey:UIImagePickerControllerMediaMetadata] objectForKey:@"{Exif}"] objectForKey:@"DateTimeOriginal"];

    // If the CLLocationManager is in fact authorized
    // and locations have been found...
    if (self.locationDictionary.allKeys.count > 0) {

        // Sort the location dictionary timestamps in ascending order
        NSArray *sortedKeys = [[self.locationDictionary allKeys] sortedArrayUsingSelector: @selector(compare:)];

        // As a default, set the selected photo's CLLocation class
        // variable to contain the first value in the sorted dictionary
        self.selectedPhotoLocation = [self.locationDictionary objectForKey:[sortedKeys objectAtIndex:0]];

        // Then go through the location dictionary and set the
        // photo location to whatever value in the dictionary
        // has a key containing a time most closely before
        // the image timestamp. Note that the keys can be compared
        // as strings since they're formatted in descending order --
        // year to month to day to hour to minute to second.
        for (NSString *key in sortedKeys) {

            // If the photo's metadata timestamp is less than or equal to
            // the current key, set the selected photo's location class
            // variable to contain the CLLocation value associated with the key
            if ([self.selectedPhotoDateTime compare:key] != NSOrderedAscending) {
                self.selectedPhotoLocation = [self.locationDictionary objectForKey:key];
            }

            // Else if the time in the location dictionary is past
            // the photo's timestamp, break from the loop
            else {
                break;
            }
        }
    }

    [self dismissViewControllerAnimated:YES completion:nil];

}

关于ios - 如何在 UIImagePickerController 捕获图像的瞬间获取当前位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27391564/

相关文章:

ios - Cordova 8 + iOS 4.5.4 = 找不到模块 "../cordova/platform_metadata"

ios - 在iPad上未检测到歌曲

android - 从 Facebook 获取个人资料图片并将其显示在 Android 应用程序中

ios - Objective-C - [super dealloc] 旧?

ios - 在 Realm 中删除一对多关系的项目

ios - 在 Objective-C 中基于 if else 定义宏

objective-c - 如何将CGPath保存为svg或pdf?

ios - objective-c 类的命名约定

iphone - 如何在 iPhone 的 MFMailComposeViewController 中将图像嵌入 html

image - Octave ,套接字连接收到的显示图像不显示