iOS模拟器添加带有定位信息的照片

时间:2015-04-23 14:59:00

标签: ios objective-c ios-simulator photo alassetslibrary

我正在使用Xcode Simulator 8.2版(最新版)测试我的应用程序。当用户选择照片时,我的代码使用ALAssetsLibrary提取CLocation信息。特别是属性ALAssetPropertyLocation。

我该如何测试?我知道如何将照片添加到模拟器中。我用iPhone拍了一张照片,然后将照片拖放到模拟器上,将其放入模拟器的相机胶卷中。在我将照片放入模拟器之前,我还验证了它有EXIF位置数据。

代码看起来正确,但我猜测照片的元数据可能存储在其他地方,只是将照片复制到模拟器上就不会创建CLocation信息。

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

    if ([picker sourceType] == UIImagePickerControllerSourceTypePhotoLibrary) {

         // We'll store the info to use in another function later
        self.selectedPhotoInfo = info;

    // Get picked image from info dictionary
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

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

            // call image location handler
            [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];
        [assetslibrary assetForURL:url
                       resultBlock:resultblock
                      failureBlock:failureblock];
      }

2 个答案:

答案 0 :(得分:0)

尝试通过将'myasset'传递给此方法来获取CLLocation。

- (CLLocation *)getLocationOfImageAsset:(ALAsset *)imageAsset {
    ALAssetRepresentation *rep = [imageAsset defaultRepresentation];
    NSDictionary *metadata = rep.metadata;
    if ([metadata objectForKey:@"{GPS}"]) {
        return [[CLLocation alloc] initWithLatitude:[[[metadata objectForKey:@"{GPS}"] objectForKey:@"Latitude"] doubleValue] longitude:[[[metadata objectForKey:@"{GPS}"] objectForKey:@"Longitude"] doubleValue]];
    } else {
        return nil;
    }
}

答案 1 :(得分:0)

对于旧版本的Xcode,当照片被添加到iOS模拟器中的照片库时,它会被转换为UIImage,并使用UIImageWriteToSavedPhotosAlbum()放置在照片库中。这只是将图像数据放在库中,而不保留EXIF数据。

较新版本(我相信Xcode 7+)使用能够保留EXIF数据的较新API导入照片。