4

以下代码成功检查了用户库中所有照片的 EXIF 数据,并将在给定位置附近拍摄的照片数组以及NSDate最近拍摄的照片的数组传递给完成块在那个位置。

- (void)getPhotosAtLocation:(CLLocation *)location withCompletionBlock:(void (^)(NSError *, NSDate *, NSMutableArray *))completionBlock {
    NSMutableArray *photos = [NSMutableArray array];
    ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
    __block NSDate *latestDate = [[NSDate alloc] initWithTimeIntervalSince1970:0];
    [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
            if (result) {
                ALAssetRepresentation *representation = [result defaultRepresentation];
                NSDictionary *imageMetadata = [representation metadata];
                NSLog(@"%@", imageMetadata);
                //analyze location
                if ([imageMetadata objectForKey:@"{GPS}"]) {
                    double latitude = [[[imageMetadata objectForKey:@"{GPS}"] objectForKey:@"Latitude"] doubleValue];
                    if ([[[imageMetadata objectForKey:@"{GPS}"] objectForKey:@"LatitudeRef"] isEqualToString:@"S"])
                        latitude *= -1;
                    double longitude = [[[imageMetadata objectForKey:@"{GPS}"] objectForKey:@"Longitude"] doubleValue];
                    if ([[[imageMetadata objectForKey:@"{GPS}"] objectForKey:@"LongitudeRef"] isEqualToString:@"W"])
                        longitude *= -1;
                    if (fabs(location.coordinate.latitude - latitude) <= 0.0005 && fabs(location.coordinate.longitude - longitude) <= 0.0005)
                        [photos addObject:[UIImage imageWithCGImage:[result thumbnail]]];
                    //analyze last time at location
                    if ([imageMetadata objectForKey:@"{TIFF}"]) {
                        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                        [formatter setDateFormat:@"yyyy:MM:dd HH:mm:ss"];
                        NSDate *tempDate = [[NSDate alloc] init];
                        tempDate = [formatter dateFromString:[[imageMetadata objectForKey:@"{TIFF}"] objectForKey:@"DateTime"]];
                        if ([tempDate compare:latestDate] == NSOrderedDescending) {
                            latestDate = tempDate;
                        }
                    }
                }
            }
        }];
        if ([latestDate isEqualToDate:[NSDate dateWithTimeIntervalSince1970:0]])
            completionBlock(nil, [NSDate date], photos);
        else
            completionBlock(nil, latestDate, photos);
    } failureBlock:^(NSError *error) {
        completionBlock(error, nil, nil);
    }];
}

我面临的问题是,为了找到在特定位置拍摄的所有照片,我必须对用户相机胶卷中的所有照片进行线性迭代(花费 O(n) 时间)。是否可以通过让系统在迭代之前按纬度和经度对照片进行排序来加快此过程?什么样的算法可以最大限度地减少返回相关图像所需的时间?

4

2 回答 2

0

我认为您应该遍历所有照片一次,并从您已经处理的照片中保留一系列属性。这样,下次迭代时,只会处理新照片。

于 2013-08-19T14:31:44.243 回答
0

您似乎正在寻找某种空间索引。在“空间索引”部分查看可能性列表:http ://en.wikipedia.org/wiki/Spatial_database 。

于 2013-08-17T19:10:30.760 回答