使用GPS元数据的UIImagePicker到NSData而不保存到相册

时间:2013-06-05 22:20:58

标签: ios objective-c uiimagepickercontroller

我已设法通过以下步骤将GPS坐标添加到元数据中:

  1. 从UIImagePickerControllerSourceTypeCamera
  2. 检索UIIMage
  3. 使用地理位置化元数据保存到磁盘(writeImageToSavedPhotosAlbum
  4. 使用ALAsset从磁盘中读取并将其转换为jpeg NSData
  5. 如果没有保存/读取到磁盘的麻烦,我可以这样做吗?

2 个答案:

答案 0 :(得分:1)

你可以使用Quartz做一些困难。您可以从UIImage获取CGImage,将其与元数据结合以创建新的CGImage。 This post进行一次此类讨论,但通过搜索Quartz名称和术语"元数据"应该导致其他人。

答案 1 :(得分:1)

UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self dismissModalViewControllerAnimated:YES];

    [Utils ifLocationEnabledDo:^{
        switch (picker.sourceType) {
            case UIImagePickerControllerSourceTypeCamera:
            {   
                UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
                NSMutableDictionary *metadata = [[NSMutableDictionary alloc] initWithDictionary:[info objectForKey:UIImagePickerControllerMediaMetadata]];
                [Utils setLocation:metadata location:[SharedAppDelegate getCurrentLocation]];
                [[Utils getAssetsLibrary] writeImageDataToSavedPhotosAlbum:image metadata:metadata completionBlock:nil];
            }
                break;
            default:
                break;
        }
    }];
}

在我的Utils课程中:

+ (void)setLocation:(NSMutableDictionary *)metadata location:(CLLocation *)location
{

    if (location) {

        CLLocationDegrees exifLatitude  = location.coordinate.latitude;
        CLLocationDegrees exifLongitude = location.coordinate.longitude;

        NSString *latRef;
        NSString *lngRef;
        if (exifLatitude < 0.0) {
            exifLatitude = exifLatitude * -1.0f;
            latRef = @"S";
        } else {
            latRef = @"N";
        }

        if (exifLongitude < 0.0) {
            exifLongitude = exifLongitude * -1.0f;
            lngRef = @"W";
        } else {
            lngRef = @"E";
        }

        NSMutableDictionary *locDict = [[NSMutableDictionary alloc] init];
        if ([metadata objectForKey:(NSString*)kCGImagePropertyGPSDictionary]) {
            [locDict addEntriesFromDictionary:[metadata objectForKey:(NSString*)kCGImagePropertyGPSDictionary]];
        }
        [locDict setObject:[self getUTCFormattedDate:location.timestamp] forKey:(NSString*)kCGImagePropertyGPSTimeStamp];
        [locDict setObject:latRef forKey:(NSString*)kCGImagePropertyGPSLatitudeRef];
        [locDict setObject:[NSNumber numberWithFloat:exifLatitude] forKey:(NSString*)kCGImagePropertyGPSLatitude];
        [locDict setObject:lngRef forKey:(NSString*)kCGImagePropertyGPSLongitudeRef];
        [locDict setObject:[NSNumber numberWithFloat:exifLongitude] forKey:(NSString*)kCGImagePropertyGPSLongitude];
        [locDict setObject:[NSNumber numberWithFloat:location.horizontalAccuracy] forKey:(NSString*)kCGImagePropertyGPSDOP];
        [locDict setObject:[NSNumber numberWithFloat:location.altitude] forKey:(NSString*)kCGImagePropertyGPSAltitude];

        [metadata setObject:locDict forKey:(NSString*)kCGImagePropertyGPSDictionary];
    }
}