我有一个启用了定位服务的应用程序(来自设置等),我正在使用 ALAssetsLibrary 从我的设备读取所有照片。
它在 iPhone4 iOS 4.2.1 上完美运行,但相同的代码和相同的 Assets 在任何 3gs 任何 iOS(从 4.0 到 4.2.1)上都不起作用。
问题是它在 3gs 上总是检索到无效的 CLLocation。
即使在 iPhone4 上的同一张图片(跨两个设备复制)上返回有效的 CLLocation,在 3gs 上它返回无效! 图片图像本身在两个设备上都有效,只有 CLLocation 无效。我没有任何崩溃,代码也没有进入任何无效 block ,除了无效的 CLLocation 数据外,一切正常。
相同 Assets 和相同代码以及相同 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;
在我的 View Controller 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!");
}];
最后一件事,在 native 照片应用程序上,图片显示在 3gs 上的正确位置,因此它们必须在某个地方拥有该数据,它只是无法以某种方式读取它!
我不完全确定这是一个设备 (3gs) 问题,通常它从来都不是......
最佳答案
在 iOS 4.0 及更高版本中,您可以尝试使用类似这样的方法对 ALAsset 进行操作并检索通过枚举或使用 assetForURL
检索的照片 Assets 的位置数据(以及更多) :
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(或元数据)并查看要使用的键。
请记住,数据并不总是存在(在飞行模式下拍摄的照片),因此请务必检查这些值。
关于iphone - ALAssetPropertyLocation 不适用于 3gs iPhone 上的任何 iOS 4.0+,但在 iPhone4 上运行完美,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5050334/