Commit 570eedc8 authored by yuzheng.tz's avatar yuzheng.tz

拍照时将定位信息存储到照片中

parent 541a0a7f
......@@ -27,6 +27,7 @@
900EF00F1C2C0B1100EA709B /* TZAssetModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 900EF00E1C2C0B1100EA709B /* TZAssetModel.m */; };
900EF0121C2C107400EA709B /* TZPhotoPreviewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 900EF0111C2C107400EA709B /* TZPhotoPreviewController.m */; };
900EF0161C2C134900EA709B /* TZPhotoPreviewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 900EF0141C2C134900EA709B /* TZPhotoPreviewCell.m */; };
902938D81EE2D02C000F2F8F /* TZLocationManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 902938D71EE2D02C000F2F8F /* TZLocationManager.m */; };
9038D5911C3974F0007DE549 /* TZTestCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 9038D5901C3974F0007DE549 /* TZTestCell.m */; };
90CE84AE1C3A89EF003D0779 /* TZImageManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 90CE84AD1C3A89EF003D0779 /* TZImageManager.m */; };
90CE84B71C3BABB6003D0779 /* TZVideoPlayerController.m in Sources */ = {isa = PBXBuildFile; fileRef = 90CE84B61C3BABB6003D0779 /* TZVideoPlayerController.m */; };
......@@ -92,6 +93,8 @@
900EF0111C2C107400EA709B /* TZPhotoPreviewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TZPhotoPreviewController.m; sourceTree = "<group>"; };
900EF0131C2C134800EA709B /* TZPhotoPreviewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TZPhotoPreviewCell.h; sourceTree = "<group>"; };
900EF0141C2C134900EA709B /* TZPhotoPreviewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TZPhotoPreviewCell.m; sourceTree = "<group>"; };
902938D61EE2D02C000F2F8F /* TZLocationManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TZLocationManager.h; sourceTree = "<group>"; };
902938D71EE2D02C000F2F8F /* TZLocationManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TZLocationManager.m; sourceTree = "<group>"; };
9038D58F1C3974F0007DE549 /* TZTestCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TZTestCell.h; sourceTree = "<group>"; };
9038D5901C3974F0007DE549 /* TZTestCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TZTestCell.m; sourceTree = "<group>"; };
90CE84AC1C3A89EF003D0779 /* TZImageManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TZImageManager.h; sourceTree = "<group>"; };
......@@ -227,6 +230,8 @@
90CE84AD1C3A89EF003D0779 /* TZImageManager.m */,
6DC84D081DF5358500A107A9 /* TZImageCropManager.h */,
6DC84D091DF5358500A107A9 /* TZImageCropManager.m */,
902938D61EE2D02C000F2F8F /* TZLocationManager.h */,
902938D71EE2D02C000F2F8F /* TZLocationManager.m */,
90EBF5D41C2E298000CB9BCC /* UIView+Layout.h */,
90EBF5D51C2E298000CB9BCC /* UIView+Layout.m */,
6D5358CB1D64600F00928CC6 /* NSBundle+TZImagePicker.h */,
......@@ -386,6 +391,7 @@
900EF0121C2C107400EA709B /* TZPhotoPreviewController.m in Sources */,
6D5358CD1D64600F00928CC6 /* NSBundle+TZImagePicker.m in Sources */,
900EEFF81C2BBF9500EA709B /* TZImagePickerController.m in Sources */,
902938D81EE2D02C000F2F8F /* TZLocationManager.m in Sources */,
6DC84D0A1DF5358500A107A9 /* TZImageCropManager.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
......
......@@ -26,6 +26,8 @@
<string>访问相机以拍照</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>访问相册以选择照片</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>允许定位以把位置保存到照片中</string>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
......
......@@ -66,6 +66,7 @@
/// Save photo 保存照片
- (void)savePhotoWithImage:(UIImage *)image completion:(void (^)(NSError *error))completion;
- (void)savePhotoWithImage:(UIImage *)image location:(CLLocation *)location completion:(void (^)(NSError *error))completion;
/// Get video 获得视频
- (void)getVideoWithAsset:(id)asset completion:(void (^)(AVPlayerItem * playerItem, NSDictionary * info))completion;
......
......@@ -558,12 +558,21 @@ static CGFloat TZScreenScale;
#pragma mark - Save photo
- (void)savePhotoWithImage:(UIImage *)image completion:(void (^)(NSError *error))completion {
[self savePhotoWithImage:image location:nil completion:completion];
}
- (void)savePhotoWithImage:(UIImage *)image location:(CLLocation *)location completion:(void (^)(NSError *error))completion {
NSData *data = UIImageJPEGRepresentation(image, 0.9);
if (iOS9Later) { // 这里有坑... iOS8系统下这个方法保存图片会失败 原来是因为PHAssetResourceType是iOS9之后的...
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetResourceCreationOptions *options = [[PHAssetResourceCreationOptions alloc] init];
options.shouldMoveFile = YES;
[[PHAssetCreationRequest creationRequestForAsset] addResourceWithType:PHAssetResourceTypePhoto data:data options:options];
PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAsset];
[request addResourceWithType:PHAssetResourceTypePhoto data:data options:options];
if (location) {
request.location = location;
}
request.creationDate = [NSDate date];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
dispatch_sync(dispatch_get_main_queue(), ^{
if (success && completion) {
......
......@@ -4,7 +4,7 @@
//
// Created by 谭真 on 15/12/24.
// Copyright © 2015年 谭真. All rights reserved.
// version 1.7.9 - 2017.04.01
// version 1.8.0 - 2017.06.03
/*
经过测试,比起xib的方式,把TZAssetCell改用纯代码的方式来写,滑动帧数明显提高了(约提高10帧左右)
......
......@@ -4,7 +4,7 @@
//
// Created by 谭真 on 15/12/24.
// Copyright © 2015年 谭真. All rights reserved.
// version 1.7.9 - 2017.04.01
// version 1.8.0 - 2017.06.03
#import "TZImagePickerController.h"
#import "TZPhotoPickerController.h"
......
//
// TZLocationManager.h
// TZImagePickerController
//
// Created by 谭真 on 2017/06/03.
// Copyright © 2017年 谭真. All rights reserved.
// 定位管理类
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface TZLocationManager : NSObject
+ (instancetype)manager;
/// 开始定位
- (void)startLocation;
- (void)startLocationWithSuccessBlock:(void (^)(CLLocation *location,CLLocation *oldLocation))successBlock failureBlock:(void (^)(NSError *error))failureBlock;
- (void)startLocationWithGeocoderBlock:(void (^)(NSArray *geocoderArray))geocoderBlock;
- (void)startLocationWithSuccessBlock:(void (^)(CLLocation *location,CLLocation *oldLocation))successBlock failureBlock:(void (^)(NSError *error))failureBlock geocoderBlock:(void (^)(NSArray *geocoderArray))geocoderBlock;
@end
//
// TZLocationManager.m
// TZImagePickerController
//
// Created by 谭真 on 2017/06/03.
// Copyright © 2017年 谭真. All rights reserved.
// 定位管理类
#import "TZLocationManager.h"
#import "TZImagePickerController.h"
@interface TZLocationManager ()<CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
/// 定位成功的回调block
@property (nonatomic, copy) void (^successBlock)(CLLocation *location,CLLocation *oldLocation);
/// 编码成功的回调block
@property (nonatomic, copy) void (^geocodeBlock)(NSArray *geocodeArray);
/// 定位失败的回调block
@property (nonatomic, copy) void (^failureBlock)(NSError *error);
@end
@implementation TZLocationManager
+ (instancetype)manager {
static TZLocationManager *manager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[self alloc] init];
manager.locationManager = [[CLLocationManager alloc] init];
manager.locationManager.delegate = manager;
if (iOS8Later) {
[manager.locationManager requestWhenInUseAuthorization];
}
});
return manager;
}
- (void)startLocation {
[self startLocationWithSuccessBlock:nil failureBlock:nil geocoderBlock:nil];
}
- (void)startLocationWithSuccessBlock:(void (^)(CLLocation *location,CLLocation *oldLocation))successBlock failureBlock:(void (^)(NSError *error))failureBlock {
[self startLocationWithSuccessBlock:successBlock failureBlock:failureBlock geocoderBlock:nil];
}
- (void)startLocationWithGeocoderBlock:(void (^)(NSArray *geocoderArray))geocoderBlock {
[self startLocationWithSuccessBlock:nil failureBlock:nil geocoderBlock:geocoderBlock];
}
- (void)startLocationWithSuccessBlock:(void (^)(CLLocation *location,CLLocation *oldLocation))successBlock failureBlock:(void (^)(NSError *error))failureBlock geocoderBlock:(void (^)(NSArray *geocoderArray))geocoderBlock {
[self.locationManager startUpdatingLocation];
_successBlock = successBlock;
_geocodeBlock = geocoderBlock;
_failureBlock = failureBlock;
}
#pragma mark - CLLocationManagerDelegate
/// 地理位置发生改变时触发
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[manager stopUpdatingLocation];
if (_successBlock) {
_successBlock(newLocation,oldLocation);
}
if (_geocodeBlock) {
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *array, NSError *error) {
_geocodeBlock(array);
}];
}
}
/// 定位失败回调方法
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"定位失败, 错误: %@",error);
switch([error code]) {
case kCLErrorDenied: { // 用户禁止了定位权限
} break;
default: break;
}
if (_failureBlock) {
_failureBlock(error);
}
}
@end
......@@ -15,6 +15,7 @@
#import "TZImageManager.h"
#import "TZVideoPlayerController.h"
#import "TZGifPhotoPreviewController.h"
#import "TZLocationManager.h"
@interface TZPhotoPickerController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIAlertViewDelegate> {
NSMutableArray *_models;
......@@ -33,6 +34,7 @@
@property (nonatomic, assign) BOOL isSelectOriginalPhoto;
@property (nonatomic, strong) TZCollectionView *collectionView;
@property (nonatomic, strong) UIImagePickerController *imagePickerVc;
@property (strong, nonatomic) CLLocation *location;
@end
static CGSize AssetGridThumbnailSize;
......@@ -519,6 +521,13 @@ static CGSize AssetGridThumbnailSize;
// 调用相机
- (void)pushImagePickerController {
// 提前定位
[[TZLocationManager manager] startLocationWithSuccessBlock:^(CLLocation *location, CLLocation *oldLocation) {
_location = location;
} failureBlock:^(NSError *error) {
_location = nil;
}];
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
self.imagePickerVc.sourceType = sourceType;
......@@ -640,13 +649,14 @@ static CGSize AssetGridThumbnailSize;
if ([type isEqualToString:@"public.image"]) {
TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController;
[imagePickerVc showProgressHUD];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
if (image) {
[[TZImageManager manager] savePhotoWithImage:image completion:^(NSError *error){
UIImage *photo = [info objectForKey:UIImagePickerControllerOriginalImage];
if (photo) {
[[TZImageManager manager] savePhotoWithImage:photo location:self.location completion:^(NSError *error){
if (!error) {
[self reloadPhotoArray];
}
}];
self.location = nil;
}
}
}
......
......@@ -17,6 +17,7 @@
#import "TZVideoPlayerController.h"
#import "TZPhotoPreviewController.h"
#import "TZGifPhotoPreviewController.h"
#import "TZLocationManager.h"
@interface ViewController ()<TZImagePickerControllerDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UIAlertViewDelegate,UINavigationControllerDelegate> {
NSMutableArray *_selectedPhotos;
......@@ -28,6 +29,7 @@
}
@property (nonatomic, strong) UIImagePickerController *imagePickerVc;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (strong, nonatomic) CLLocation *location;
// 6个设置开关
@property (weak, nonatomic) IBOutlet UISwitch *showTakePhotoBtnSwitch; ///< 在内部显示拍照按钮
@property (weak, nonatomic) IBOutlet UISwitch *sortAscendingSwitch; ///< 照片排列按修改时间升序
......@@ -297,6 +299,13 @@
// 调用相机
- (void)pushImagePickerController {
// 提前定位
[[TZLocationManager manager] startLocationWithSuccessBlock:^(CLLocation *location, CLLocation *oldLocation) {
_location = location;
} failureBlock:^(NSError *error) {
_location = nil;
}];
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
self.imagePickerVc.sourceType = sourceType;
......@@ -317,8 +326,9 @@
tzImagePickerVc.sortAscendingByModificationDate = self.sortAscendingSwitch.isOn;
[tzImagePickerVc showProgressHUD];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// save photo and get asset / 保存图片,获取到asset
[[TZImageManager manager] savePhotoWithImage:image completion:^(NSError *error){
[[TZImageManager manager] savePhotoWithImage:image location:self.location completion:^(NSError *error){
if (error) {
[tzImagePickerVc hideProgressHUD];
NSLog(@"图片保存失败 %@",error);
......@@ -351,6 +361,11 @@
[_selectedAssets addObject:asset];
[_selectedPhotos addObject:image];
[_collectionView reloadData];
if ([asset isKindOfClass:[PHAsset class]]) {
PHAsset *phAsset = asset;
NSLog(@"location:%@",phAsset.location);
}
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
......@@ -417,6 +432,12 @@
// 1.打印图片名字
[self printAssetsName:assets];
// 2.图片位置信息
if (iOS8Later) {
for (PHAsset *phAsset in assets) {
NSLog(@"location:%@",phAsset.location);
}
}
}
// If user picking a video, this callback will be called.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment