获取当前位置UIImagePickerController捕获图

获取当前位置UIImagePickerController捕获图

本文介绍了如何在即时获取当前位置UIImagePickerController捕获图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

有没有办法做到这一点?有没有一种方法来听取capturePhoto事件,例如?



为了澄清,使用我的应用程序的用户可能会移动得非常快。

解决方案

这是我推荐的,因此您不必跟踪用户的位置,因此您可以获取用户的位置



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

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

并确保它已被授权:

<$ p如果([CLLocationManager authorizationStatus]!= kCLAuthorizationStatusAuthorizedWhenInUse){
[self.locationManager requestWhenInUseAuthorization]; $ p $

$ / code>

(也包括.plist中的NSLocationWhenInUseUsageDescription键)



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

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

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

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

//格式化当前日期时间以匹配
的格式//照片的元数据timestamp字符串
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@YYYY:MM:dd HH:mm:ss];
NSString * stringFromDate = [formatter stringFromDate:[NSDate date]];

//将位置作为位置添加到位置NSMutableDictionary
//将格式化的当前日期时间用作关键字
[self.locationDictionary setValue:newLocation forKey:stringFromDate] ;
}

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

*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

[self.locationManager stopUpdatingLocation];

//当选择一张照片时,将它保存为UIImage
self.selectedPhoto = info [UIImagePickerControllerOriginalImage];

//从元数据获取时间戳,并将其作为NSString存储
self.selectedPhotoDateTime = [[[info valueForKey:UIImagePickerControllerMediaMetadata] objectForKey:@{Exif}] objectForKey:@ 原始日期时间];

//如果CLLocationManager事实上被授权
//并且已找到位置...
if(self.locationDictionary.allKeys.count> 0){

//将位置字典时间戳按升序排序
NSArray * sortedKeys = [[self.locationDictionary allKeys] sortedArrayUsingSelector:@selector(compare :)];

//默认情况下,将所选照片的​​CLLocation类
//变量设置为包含已排序字典中的第一个值
self.selectedPhotoLocation = [self.locationDictionary objectForKey: [sortedKeys objectAtIndex:0]];

//然后通过位置字典并将
//照片位置设置为字典中的任何值
//在$ b之前包含最接近时间的键$ b //图像时间戳。请注意,可以将
//作为字符串进行比较,因为它们的格式按降序排列 -
//年份到月份到日期到小时到分钟到秒。
for(NSString * key in sortedKeys){

//如果照片的元数据时间戳小于或等于
//当前键,请设置所选照片的​​位置类
//变量包含与键
相关联的CLLocation值if([self.selectedPhotoDateTime compare:key]!= NSOrderedAscending){
self.selectedPhotoLocation = [self.locationDictionary objectForKey:key ]。
}

//否则,如果位置字典中的时间超过
//照片的时间戳,请从循环中断开
else {
break ;
}
}
}

[self dismissViewControllerAnimated:YES completion:nil];

}


I have researched on how to get location data from images returned from UIImagePickerController camera. However, I think that the easiest way is to get the current location from CLLocationManager at the instant UIImagePickerController captures an image.

Is there a way of doing this? Is there a way of listening for the "capturePhoto" event, for example?

Just to clarify, the users using my app will likely be moving pretty fast.

解决方案

Here's what I'd recommend so you don't track the user's location any more than you have to and so you get the user's location closest to the time the image was actually snapped.

Instantiate the CLLocationManager class variable in your viewDidLoad, ex:

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

And make sure it's authorized:

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

(Also include the "NSLocationWhenInUseUsageDescription" key in the .plist)

Then you could wait until the UIImagePickerController is actually presented before (1) initializing the dictionary to hold the locations and (2) starting to update the location, ex:

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

At that point, you can start storing the user's updated locations in an NSMutableDictionary self.locationDictionary class instance variable when CLLocation values are returned from the didUpdateToLocation delegate method, ex:

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

And then once the image is selected, find its timestamp in the metadata and find the value in the location dictionary with a timestamp key closest to the image timestamp, ex:

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

}

这篇关于如何在即时获取当前位置UIImagePickerController捕获图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 19:37