【问题标题】:iOS Simulator Add Photo with CLocation informationiOS 模拟器添加带有 CLocation 信息的照片
【发布时间】:2015-07-01 20:45:46
【问题描述】:

我正在使用 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];
      }

【问题讨论】:

  • 发布一些代码怎么样?

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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    尝试通过将您的“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;
        }
    }
    

    【讨论】:

    • 感谢 Suttikeat 的建议。虽然我可以在调试器中遍历元数据,但在我的图像中看不到 GPS 信息。我最初的问题是如何将图像添加到具有位置信息的 Xcode 模拟器,以便我可以对此进行测试。重复一遍,我正在测试的图像来自打开位置的 iPhone,我在 PhotoShop 中验证它具有 GPS 元数据。看来,当我将图像带入 Xcode Simulator 时,GPS 数据不再出现在图像中。
    • 我从未在模拟器中测试过,但您应该尝试通过上传和下载等其他方式将照片添加到模拟器中。 stackoverflow.com/questions/29129746/…
    猜你喜欢
    • 1970-01-01
    • 2011-05-12
    • 2023-03-18
    • 2011-07-07
    • 2017-12-14
    • 2016-01-31
    • 1970-01-01
    • 2021-11-27
    • 2011-03-18
    相关资源
    最近更新 更多