获取相册里的照片拍摄地点和时间

头文件

#import <MobileCoreServices/MobileCoreServices.h>
#import <ImageIO/CGImageProperties.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <CoreLocation/CoreLocation.h>
#import "NSDictionary+CLLocation.h"

选择照片

- (IBAction)openAlbum:(UIButton *)sender {

UIImagePickerController *pickVC = [[UIImagePickerController alloc]init];
pickVC.delegate = self;
pickVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:pickVC animated:YES completion:nil];
}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{

if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    self.photoImageV.image = image;
    
    NSData* imageData = UIImagePNGRepresentation(image);
    BOOL result = [imageData writeToFile:self.cachePicPath atomically:YES];
    NSLog(@"=====%d",result);;
    [picker dismissViewControllerAnimated:YES completion:nil];
    self.imgUrl = [info objectForKey:UIImagePickerControllerReferenceURL];
}
if(picker.sourceType == UIImagePickerControllerSourceTypeCamera) {//拍照
    
    //照片mediaInfo
    NSDictionary * imageMetadata = info[@"UIImagePickerControllerMediaMetadata"];
    
    NSDictionary *tIFFDictionary =  [imageMetadata objectForKey:(NSString *)kCGImagePropertyTIFFDictionary];
    
    NSString * pictureTime = tIFFDictionary[@"DateTime"];//2016:01:05 11:45:36
    
    //self.time.text = pictureTime;
    NSLog(@"时间: %@",pictureTime);
    if ([CLLocationManager locationServicesEnabled]) {
        
        //获取经纬度
        [self getLocation];
        
    }else {
        NSLog(@"请开启定位功能!");
    }
    
} else if(picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary){//相册
    
    NSURL *assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];
    
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    
    __block NSMutableDictionary *imageMetadata_GPS = nil;

    
    [library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
        
        //获取时间
        NSDate* pictureDate = [asset valueForProperty:ALAssetPropertyDate];
        NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
        formatter.dateFormat = @"yyyy:MM:dd HH:mm:ss";
        formatter.timeZone = [NSTimeZone localTimeZone];
        NSString * pictureTime = [formatter stringFromDate:pictureDate];
        //weakSelf.time.text = pictureTime;
        NSLog(@"时间: %@",pictureTime);
        //获取GPS
        imageMetadata_GPS = [[NSMutableDictionary alloc] initWithDictionary:asset.defaultRepresentation.metadata];
        
        NSDictionary *GPSDict=[imageMetadata_GPS objectForKey:(NSString*)kCGImagePropertyGPSDictionary];
        
        if (GPSDict!=nil) {
            
            CLLocation *loc=[GPSDict locationFromGPSDictionary];
            
            //weakSelf.weidu.text = [NSString stringWithFormat:@"%f", loc.coordinate.latitude];
            //weakSelf.jingdu.text = [NSString stringWithFormat:@"%f", loc.coordinate.longitude];
            
            CLGeocoder *clGeoCoder = [[CLGeocoder alloc] init];
            
            CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:loc.coordinate.latitude longitude:loc.coordinate.longitude];
            
            //反向地理编码的请求 -> 根据经纬度 获取 位置
            [clGeoCoder reverseGeocodeLocation:newLocation completionHandler: ^(NSArray *placemarks,NSError *error) {
                for (CLPlacemark *placeMark in placemarks)
                {
                    NSDictionary *addressDic=placeMark.addressDictionary;
                    
                    NSArray *location_Arr = [addressDic objectForKey:@"FormattedAddressLines"];//系统格式化后的位置

                    NSLog(@"位置: %@",[location_Arr firstObject]);
                }
            }];
            
        }else{
            NSLog(@"此照片没有GPS信息");
      
        }
        
    }
     
     failureBlock:^(NSError *error) {
         
     }];
  }
}
展开阅读全文

没有更多推荐了,返回首页