ALAssetPropertyLocation无法在3gs iPhone上的任何iOS 4.0+上运行,但在iPhone4上可以正常运行 [英] ALAssetPropertyLocation not working on any iOS 4.0+ on 3gs iPhones but working perfectly on iPhone4

查看:179
本文介绍了ALAssetPropertyLocation无法在3gs iPhone上的任何iOS 4.0+上运行,但在iPhone4上可以正常运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个启用了位置服务的应用程序(通过设置等),并且正在使用ALAssetsLibrary从设备读取所有照片.

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

它可以在iPhone4 iOS 4.2.1上完美运行,但是相同的代码和相同的资产在任何3g的iOS(4.0至4.2.1)上都无法使用.

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).

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

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

即使在iPhone4的同一张图片(在两个设备上复制)上,它也会返回有效的CLLocation,而在3gs上,它会返回无效的CLLocation! 图片图像本身在两个设备上均有效,只有CLLocation无效.我没有崩溃,代码也没有进入任何无效的块,除了无效的CLLocation数据之外,其他一切都正常.

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.

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

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

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

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

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

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)

我正在像这样获取CLLocation:

Im getting the CLLocation like this:

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

在我的视图控制器DidLoad中调用的ALAssetsLibrary代码如下(完全标准):

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

最后一件事,在本地照片应用程序上,图片显示在3gs上的正确位置,因此它们必须在某处具有该数据,只是无法以某种方式读取它!

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!

我不能完全确定这是设备(3gs)的问题,通常永远不会...

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

推荐答案

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

In iOS 4.0 and higher, you can try using something like this to operate on the ALAsset and retrieve the location data (and more) for a photo asset that is retrieved either by enumerating or using 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(或元数据),并查看要使用的键.

The latitude and longitude Refs are directions that you will need to translate into positive (N, E) and negative (S, W). You can then use all of that information to build a simple CLLocation. If you need more, log or use the debugger to look at gpsDict (or metadata) and see which keys to use.

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

Keep in mind that the data is not always there (photos taken in airplane mode) so be sure to check the values.

这篇关于ALAssetPropertyLocation无法在3gs iPhone上的任何iOS 4.0+上运行,但在iPhone4上可以正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆