ALAssetPropertyLocation不能在3gs iPhone上的任何iOS 4.0+上运行,但在iPhone4上运行完美

Dad*_*ado 14 iphone objective-c cllocation assetslibrary

我有一个应用程序,位置服务已启用(来自设置等),我正在使用ALAssetsLibrary从我的设备中读取所有照片.

它在iPhone4 iOS 4.2.1上完美运行,但相同的代码和相同的资产在任何3gs任何iOS(从4.0到4.2.1)上都不能工作.

问题是它总是在3gs上检索无效的CLLocation.

即使在iPhone4上的相同图片(复制到两个设备上)也会返回有效的CLLocation,在3gs上它返回无效!图片图片本身在两台设备上都有效,只有CLLocation无效.我没有任何崩溃,并且代码没有进入任何无效的块,除了无效的CLLocation数据之外它都可以正常工作.

输出相同的资产相同的代码以及相同的iOS版本(4.2.1):

iPhone4:<+ 51.23816667,-0.57700000> +/- 0.00m(速度-1.00 mps /航向-1.00)@ 19/02/2011 01:59:28格林威治标准时间

3gs:<nan,nan> +/- 0.00m(速度-1.00 mps/course -1.00)@ 2011-02-19 01:20:35 +0000

(注意无效位置上的日期是当前时间)

我得到这样的CLLocation:

 CLLocation* location = [asset valueForProperty:ALAssetPropertyLocation];
 CLLocationCoordinate2D coord = [location coordinate];
 if ( !CLLocationCoordinate2DIsValid(coord) ) return;
Run Code Online (Sandbox Code Playgroud)

在我的viewcontroller DidLoad中调用的ALAssetsLibrary代码看起来像这样(完全标准):

ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];

 void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
      if(result != NULL) 
      {
                [self myFunction:result withIndex:index];

      }
 };

 void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) =  ^(ALAssetsGroup *group, BOOL *stop) {
      if(group != nil) 
      {
           numberOfAssets = [group numberOfAssets];
           [group enumerateAssetsUsingBlock:assetEnumerator];
      }
 };     

 [library enumerateGroupsWithTypes:ALAssetsGroupAll
                             usingBlock:assetGroupEnumerator
                           failureBlock: ^(NSError *error) {
                                NSLog(@"Oh no!"); 
                           }];     
Run Code Online (Sandbox Code Playgroud)

最后一点,在本机照片应用程序上,图片显示在3gs上的正确位置,所以他们必须在某处拥有该数据,它只是不以某种方式读取它!

我不完全确定它是一个设备(3gs)问题,通常它永远不会......

Mar*_*ark 1

在 iOS 4.0 及更高版本中,您可以尝试使用类似的方法对 ALAsset 进行操作,并检索通过枚举或使用检索的照片资源的位置数据(及更多)assetForURL

ALAssetRepresentation *representation = [asset defaultRepresentation];
NSDictionary *metadata = [representation metadata];
NSDictionary *gpsDict = [metadata objectForKey:@"{GPS}"];

NSNumber *latitudeNumber = [gpsDict objectForKey:@"Latitude"];
NSString *latitudeRef = [gpsDict objectForKey:@"LatitudeRef"];

NSNumber *longitudeNumber = [gpsDict objectForKey:@"Longitude"];
NSString *longitudeRef = [gpsDict objectForKey:@"LongitudeRef"];
Run Code Online (Sandbox Code Playgroud)

纬度和经度 Refs 是您需要转换为正(N、E)和负(S、W)的方向。然后,您可以使用所有这些信息来构建一个简单的 CLLocation。如果您需要更多,请记录或使用调试器查看 gpsDict (或元数据)并查看要使用哪些键。

请记住,数据并不总是存在(在飞行模式下拍摄的照片),因此请务必检查这些值。