2014-01-21 46 views
1

我有一個應用程序,用戶可以拍攝照片或從相機膠捲中選擇一張。我需要能夠從拍攝圖像的位置提取位置(長長)。目前我正在使用位置管理器來獲取設備的位置,但這可能會導致位置不準確(即有人可能會拍照但不會發郵件直到他們回家,從而改變位置)提取圖像位置數據

我找到這裏使用「資產庫」的解決方案,但是當我從相機膠捲中選擇一張照片時出現SIGBRAT錯誤。

是否有人可能會告訴我我的代碼出錯的地方,或者建議一個簡單的方法來獲取拍攝照片的位置。

下面是我的代碼:

- (IBAction)takePhoto 
{ 
    picker1 = [[UIImagePickerController alloc] init]; 
    picker1.delegate = self; 
    [picker1 setSourceType: UIImagePickerControllerSourceTypeCamera]; 
    [self presentViewController:picker1 animated:YES completion:NULL]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 

    [locationManager startUpdatingLocation]; 


} 

- (IBAction)chooseExisting 
{ 
    picker2 = [[UIImagePickerController alloc] init]; 
    picker2.delegate = self; 
    [picker2 setSourceType: UIImagePickerControllerSourceTypePhotoLibrary]; 
    [self presentViewController:picker2 animated:YES completion:NULL]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 

    [locationManager startUpdatingLocation]; 



} 


#pragma mark - CLLocationManagerDelegate 

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{ 
    NSLog(@"didFailWithError: %@", error); 
    UIAlertView *errorAlert = [[UIAlertView alloc] 
           initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [errorAlert show]; 
} 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 
    NSLog(@"didUpdateToLocation: %@", newLocation); 
    CLLocation *currentLocation = newLocation; 

    if (currentLocation != nil) { 
     longditute = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]; 
     Latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]; 
    } 
    // Stop Location Manager 
    [locationManager stopUpdatingLocation]; 
} 



- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{ 

    image = [info objectForKey:UIImagePickerControllerOriginalImage]; 
    [imageView setImage:image]; 
    [self dismissViewControllerAnimated:YES completion:NULL]; 

    assetsLibrary = [[ALAssetsLibrary alloc] init]; 
    groups = [NSMutableArray array]; 

    [assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) 
    { 
     if (group == nil) 
     { 
      return; 
     } 

     [groups addObject:group]; 

    } failureBlock:^(NSError *error) 
    { 
     // Possibly, Location Services are disabled for your application or system-wide. You should notify user to turn Location Services on. With Location Services disabled you can't access media library for security reasons. 

    }]; 
    ALAssetsGroup *group = [groups objectAtIndex:0]; 
    [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) 
    { 
     if (result == nil) 
     { 
      return; 
     } 

     // Trying to retreive location data from image 
     CLLocation *loc = [result valueForProperty:ALAssetPropertyLocation]; 
     NSLog(loc); 
    }]; 

} 

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
{ 
    [self dismissViewControllerAnimated:YES completion:NULL]; 
} 
+0

你在哪一行獲得'SIGABRT'?日誌說什麼?堆棧跟蹤說什麼? – Tommy

回答

0

你說,或者您也可以顯示抓取數據的簡單方法。

因此,我可以指出你處理這件事的兩個帖子。您首先需要確保用戶已授予應用權限,以允許位置服務獲取應用所需的詳細信息。

Nice answer given over here涵蓋了包括代碼和另一個很好的答案given here更詳細的地址 首先檢查第一個,然後第二個鏈接。

+0

我在下面一行出現聲明錯誤:'CGImageDestinationRef dr = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, ' – scb998

+1

我不知道這件事,它最好評論在我給出的鏈接中發佈答案的人你的,我的回答的目的是要把你推向我所做的正確的方向。 – Pavan