14

I've got an app with location services ENABLED (from settings etc), and I'm reading all the photos from my device using ALAssetsLibrary.

It works perfectly on iPhone4 iOS 4.2.1, but the same code and the same assets DONT WORK on any 3gs any iOS (from 4.0 to 4.2.1).

The problem is that it retrieves always an invalid CLLocation on the 3gs.

Even on the same picture (copied across both devices) on the iPhone4 returns a valid CLLocation, on the 3gs it returns invalid! The picture image itself it's valid on both devices, only the CLLocation is invalid. I dont have any crashes and the code doesnt step into any invalid blocks, it all works fine apart from the invalid CLLocation data.

Output of the same asset and same code and same iOS version (4.2.1):

iPhone4: <+51.23816667, -0.57700000> +/- 0.00m (speed -1.00 mps / course -1.00) @ 19/02/2011 01:59:28 Greenwich Mean Time

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

(Note also the date on the invalid location is the current time)

Im getting the CLLocation like this:

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

The ALAssetsLibrary code called in my viewcontroller DidLoad looks like this (totally standard):

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!"); 
                           }];     

One last thing, on the native photo app the pictures are shown in the correct location on the 3gs, so they must have that data somewhere, it just doesnt read it somehow!

I'm not entirely sure it's a device (3gs) problem, normally it never is...

4

1 回答 1

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

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

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

于 2012-11-10T18:05:18.763 回答