我有一个启用了位置服务(从设置等)的应用程序,并且我正在使用ALAssetsLibrary从我的设备读取所有照片。
它在iPhone4 iOS 4.2.1上完美运行,但是相同的代码和相同的资产在任何3gs上的任何iOS上都无法工作(从4.0到4.2.1)。
问题在于它始终在3gs上检索到无效的CLLocation。
即使在相同的图片(在两个设备上都复制)上,iPhone4返回有效的CLLocation,而在3gs上则返回无效的CLLocation!图片本身在两个设备上都是有效的,只有CLLocation是无效的。我没有任何崩溃,而且代码也没有进入任何无效的块,除了无效的CLLocation数据之外,一切正常。
相同的资产、相同的代码和相同的(4.2.1)输出:
iPhone4: <+51.23816667, -0.57700000> +/- 0.00m (speed -1.00 mps / course -1.00) @ 19/02/2011 01:59:28格林威治标准时间
3gs: < nan,nan > +/- 0.00m(速度-1.00 mps / 课程-1.00)@ 2011年2月19日01:20:35 +0000
(还要注意无效位置上的日期是当前时间)
我是这样得到CLLocation的:
CLLocation* location = [asset valueForProperty:ALAssetPropertyLocation];
CLLocationCoordinate2D coord = [location coordinate];
if ( !CLLocationCoordinate2DIsValid(coord) ) return;
在我的视图控制器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!");
}];
还有一件事,在本地照片应用中,3gs上的图片显示在正确的位置,所以它们一定有这些数据,只是不知何故没有读取出来!
我不完全确定这是设备(3gs)的问题,通常情况下并不是...