部分境外城市反編碼顯示北大西洋;窳小Q肚L指<退!
參考
iOS 獲得圖片的時(shí)間(通過拍照和取出)
如何獲取iPhone拍攝的png照片的拍攝時(shí)間,位置信息等圖片屬性?
簡單理念
- 拍照獲取
拍照即可通過CoreLocation獲取當(dāng)前位置和當(dāng)前時(shí)間晾匠。- 相冊獲取
相冊即可通過ALAssetsLibrary來獲取拍攝時(shí)間和拍攝地點(diǎn)茶袒。
本軟件是通過imagePicker的代理回調(diào)中獲取info,并從info中獲取大量有關(guān)的信息凉馆,部分圖片沒有是因?yàn)榕恼諘r(shí)沒有錄入信息薪寓,存在無網(wǎng)絡(luò)和關(guān)閉權(quán)限功能。
引入頭文件
#import <MobileCoreServices/MobileCoreServices.h>
#import <ImageIO/CGImageProperties.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <CoreLocation/CoreLocation.h>
#import "NSDictionary+CLLocation.h"
定義屬性
@property (nonatomic, strong) UIImagePickerController *imagePickerVC;
@property (nonatomic, strong) CLLocationManager *locationmanager;//拍照定位
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *time;
@property (weak, nonatomic) IBOutlet UILabel *jingdu;
@property (weak, nonatomic) IBOutlet UILabel *weidu;
@property (weak, nonatomic) IBOutlet UILabel *location;
@property (nonatomic, copy) NSString *locationFormat;
懶加載
#pragma mark 懶加載
- (UIImagePickerController *)imagePickerVC {
if (!_imagePickerVC) {
_imagePickerVC = [[UIImagePickerController alloc] init];
// 設(shè)置資源來源(相冊澜共、相機(jī)向叉、圖庫之一)
// imagePickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
// 設(shè)置可用的媒體類型、默認(rèn)只包含kUTTypeImage嗦董,如果想選擇視頻母谎,請?zhí)砑觡UTTypeMovie
// 如果選擇的是視屏,允許的視屏?xí)r長為20秒
_imagePickerVC.videoMaximumDuration = 20;
// 允許的視屏質(zhì)量(如果質(zhì)量選取的質(zhì)量過高京革,會(huì)自動(dòng)降低質(zhì)量)
_imagePickerVC.videoQuality = UIImagePickerControllerQualityTypeHigh;
_imagePickerVC.mediaTypes = @[(NSString *)kUTTypeMovie, (NSString *)kUTTypeImage];
// 設(shè)置代理奇唤,遵守UINavigationControllerDelegate, UIImagePickerControllerDelegate 協(xié)議
_imagePickerVC.delegate = self;
// 是否允許編輯(YES:圖片選擇完成進(jìn)入編輯模式)
// _imagePickerVC.allowsEditing = YES;
}
return _imagePickerVC;
}
-(CLLocationManager *)locationManager {
if (!_locationManager) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.distanceFilter = 1000.0f;
}
return _locationManager;
}
拍照相關(guān)代碼
-(void)getLocation
{
//判斷定位功能是否打開
if ([CLLocationManager locationServicesEnabled]) {
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
[self.locationManager requestAlwaysAuthorization];
[self.locationManager requestWhenInUseAuthorization];
//設(shè)置尋址精度
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = 5.0;
[self.locationManager startUpdatingLocation];
}
}
//定位失敗后調(diào)用此代理方法
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{}
//獲取一次定位,然后關(guān)掉manager
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
//防止多次調(diào)用
CLLocation *currentLocation = [locations lastObject];
NSTimeInterval locationAge = -[currentLocation.timestamp timeIntervalSinceNow];
if (locationAge > 5.0) return;
if (currentLocation.horizontalAccuracy < 0) return;
//當(dāng)前經(jīng)緯度
self.jingdu.text = [NSString stringWithFormat:@"%f", currentLocation.coordinate.longitude];
self.weidu.text = [NSString stringWithFormat:@"%f", currentLocation.coordinate.latitude];
CLGeocoder *clGeoCoder = [[CLGeocoder alloc] init];
CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:currentLocation.coordinate.latitude longitude:currentLocation.coordinate.longitude];
__weak typeof(self)weakSelf = self;
//反向地理編碼的請求 -> 根據(jù)經(jīng)緯度 獲取 位置
[clGeoCoder reverseGeocodeLocation:newLocation completionHandler: ^(NSArray *placemarks,NSError *error) {
for (CLPlacemark *placeMark in placemarks)
{
NSDictionary *addressDic=placeMark.addressDictionary;
NSArray *location_Arr = [addressDic objectForKey:@"FormattedAddressLines"];//系統(tǒng)格式化后的位置
weakSelf.location.text = [location_Arr firstObject];
}
}];
[self.locationManager stopUpdatingLocation];
}
相冊相關(guān)代碼
{//相冊
NSURL *assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__block NSMutableDictionary *imageMetadata_GPS = nil;
__weak typeof(self)weakSelf = self;
[library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
//獲取時(shí)間
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;
//獲取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];
//反向地理編碼的請求 -> 根據(jù)經(jīng)緯度 獲取 位置
[clGeoCoder reverseGeocodeLocation:newLocation completionHandler: ^(NSArray *placemarks,NSError *error) {
for (CLPlacemark *placeMark in placemarks)
{
NSDictionary *addressDic=placeMark.addressDictionary;
NSArray *location_Arr = [addressDic objectForKey:@"FormattedAddressLines"];//系統(tǒng)格式化后的位置
weakSelf.location.text = [location_Arr firstObject];
}
}];
}else{
weakSelf.weidu.text = @"此照片沒有GPS信息";
weakSelf.jingdu.text = @"此照片沒有GPS信息";
weakSelf.location.text = @"此照片沒有拍攝位置";
}
}
failureBlock:^(NSError *error) {
}];
}
demo地址
https://github.com/pengshengsongcode/ThroughPicGetShootPlaceWithTime