Commit fd1a2b2d authored by tanzhen's avatar tanzhen

可把拍照按钮放在外面了

parent b53c5298
No preview for this file type
Pod::Spec.new do |s| Pod::Spec.new do |s|
s.name = "TZImagePickerController" s.name = "TZImagePickerController"
s.version = "1.4.5" s.version = "1.5.0"
s.summary = "A clone of UIImagePickerController, support picking multiple photos、original photo and video" s.summary = "A clone of UIImagePickerController, support picking multiple photos、original photo and video"
s.homepage = "https://github.com/banchichen/TZImagePickerController" s.homepage = "https://github.com/banchichen/TZImagePickerController"
s.license = "MIT" s.license = "MIT"
s.author = { "banchichen" => "736420282@qq.com" } s.author = { "banchichen" => "736420282@qq.com" }
s.platform = :ios s.platform = :ios
s.ios.deployment_target = "6.0" s.ios.deployment_target = "6.0"
s.source = { :git => "https://github.com/banchichen/TZImagePickerController.git", :tag => "1.4.5" } s.source = { :git => "https://github.com/banchichen/TZImagePickerController.git", :tag => "1.5.0" }
s.requires_arc = true s.requires_arc = true
s.resources = "TZImagePickerController/TZImagePickerController/*.{png,xib,nib,bundle}" s.resources = "TZImagePickerController/TZImagePickerController/*.{png,xib,nib,bundle}"
s.source_files = "TZImagePickerController/TZImagePickerController/*.{h,m}" s.source_files = "TZImagePickerController/TZImagePickerController/*.{h,m}"
......
...@@ -113,10 +113,10 @@ ...@@ -113,10 +113,10 @@
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="uHt-bT-lBZ"> <switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="uHt-bT-lBZ">
<rect key="frame" x="225" y="6" width="51" height="31"/> <rect key="frame" x="225" y="6" width="51" height="31"/>
<connections> <connections>
<action selector="showSheetSwitchClick:" destination="BYZ-38-t0r" eventType="valueChanged" id="sRv-xW-VSh"/> <action selector="showSheetSwitchClick:" destination="BYZ-38-t0r" eventType="valueChanged" id="kmV-vK-SDz"/>
</connections> </connections>
</switch> </switch>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="把拍照按钮放在外面(未实现)" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="jop-5R-ioj"> <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="把拍照按钮放在外面" textAlignment="right" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="jop-5R-ioj">
<rect key="frame" x="0.0" y="0.0" width="200" height="44"/> <rect key="frame" x="0.0" y="0.0" width="200" height="44"/>
<fontDescription key="fontDescription" type="system" pointSize="15"/> <fontDescription key="fontDescription" type="system" pointSize="15"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/> <color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
<key>CFBundlePackageType</key> <key>CFBundlePackageType</key>
<string>APPL</string> <string>APPL</string>
<key>CFBundleShortVersionString</key> <key>CFBundleShortVersionString</key>
<string>1.4.5</string> <string>1.5.0</string>
<key>CFBundleSignature</key> <key>CFBundleSignature</key>
<string>????</string> <string>????</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
// //
// Created by 谭真 on 15/12/24. // Created by 谭真 on 15/12/24.
// Copyright © 2015年 谭真. All rights reserved. // Copyright © 2015年 谭真. All rights reserved.
// version 1.4.5 // version 1.5.0
/* /*
经过测试,比起xib的方式,把TZAssetCell改用纯代码的方式来写,滑动帧数明显提高了(约提高10帧左右) 经过测试,比起xib的方式,把TZAssetCell改用纯代码的方式来写,滑动帧数明显提高了(约提高10帧左右)
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
#import "TZImageManager.h" #import "TZImageManager.h"
#import "TZVideoPlayerController.h" #import "TZVideoPlayerController.h"
@interface TZPhotoPickerController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate> { @interface TZPhotoPickerController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIAlertViewDelegate> {
NSMutableArray *_models; NSMutableArray *_models;
UIButton *_previewButton; UIButton *_previewButton;
...@@ -357,10 +357,10 @@ static CGSize AssetGridThumbnailSize; ...@@ -357,10 +357,10 @@ static CGSize AssetGridThumbnailSize;
} }
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
// take a picture / 去拍照 // take a photo / 去拍照
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController; TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
if (((tzImagePickerVc.sortAscendingByModificationDate && indexPath.row >= _models.count) || (!tzImagePickerVc.sortAscendingByModificationDate && indexPath.row == 0)) && _showTakePhotoBtn) { if (((tzImagePickerVc.sortAscendingByModificationDate && indexPath.row >= _models.count) || (!tzImagePickerVc.sortAscendingByModificationDate && indexPath.row == 0)) && _showTakePhotoBtn) {
[self takePicture]; return; [self takePhoto]; return;
} }
// preview phote or video / 预览照片或视频 // preview phote or video / 预览照片或视频
NSInteger index = indexPath.row; NSInteger index = indexPath.row;
...@@ -395,7 +395,13 @@ static CGSize AssetGridThumbnailSize; ...@@ -395,7 +395,13 @@ static CGSize AssetGridThumbnailSize;
#pragma mark - Private Method #pragma mark - Private Method
- (void)takePicture { - (void)takePhoto {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if ((authStatus == AVAuthorizationStatusRestricted || authStatus ==AVAuthorizationStatusDenied) && iOS8Later) {
// 无权限 做一个友好的提示
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"无法使用相机" message:@"请在iPhone的""设置-隐私-相机""中允许访问相机" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"设置", nil];
[alert show];
} else { // 调用相机
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera; UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) { if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
self.imagePickerVc.sourceType = sourceType; self.imagePickerVc.sourceType = sourceType;
...@@ -406,6 +412,7 @@ static CGSize AssetGridThumbnailSize; ...@@ -406,6 +412,7 @@ static CGSize AssetGridThumbnailSize;
} else { } else {
NSLog(@"模拟器中无法打开照相机,请在真机中使用"); NSLog(@"模拟器中无法打开照相机,请在真机中使用");
} }
}
} }
- (void)refreshBottomToolBarStatus { - (void)refreshBottomToolBarStatus {
...@@ -487,6 +494,18 @@ static CGSize AssetGridThumbnailSize; ...@@ -487,6 +494,18 @@ static CGSize AssetGridThumbnailSize;
} }
} }
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) { // 去设置界面,开启相机访问权限
if (iOS8Later) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
} else {
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy&path=Photos"]];
}
}
}
#pragma mark - UIImagePickerControllerDelegate #pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { - (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
#import "TZImageManager.h" #import "TZImageManager.h"
#import "TZVideoPlayerController.h" #import "TZVideoPlayerController.h"
@interface ViewController ()<TZImagePickerControllerDelegate,UICollectionViewDataSource,UICollectionViewDelegate> { @interface ViewController ()<TZImagePickerControllerDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UIAlertViewDelegate> {
NSMutableArray *_selectedPhotos; NSMutableArray *_selectedPhotos;
NSMutableArray *_selectedAssets; NSMutableArray *_selectedAssets;
BOOL _isSelectOriginalPhoto; BOOL _isSelectOriginalPhoto;
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
CGFloat _margin; CGFloat _margin;
LxGridViewFlowLayout *_layout; LxGridViewFlowLayout *_layout;
} }
@property (nonatomic, strong) UIImagePickerController *imagePickerVc;
@property (nonatomic, strong) UICollectionView *collectionView; @property (nonatomic, strong) UICollectionView *collectionView;
// 6个设置开关 // 6个设置开关
@property (weak, nonatomic) IBOutlet UISwitch *showTakePhotoBtnSwitch; ///< 在内部显示拍照按钮 @property (weak, nonatomic) IBOutlet UISwitch *showTakePhotoBtnSwitch; ///< 在内部显示拍照按钮
...@@ -37,6 +38,27 @@ ...@@ -37,6 +38,27 @@
@implementation ViewController @implementation ViewController
- (UIImagePickerController *)imagePickerVc {
if (_imagePickerVc == nil) {
_imagePickerVc = [[UIImagePickerController alloc] init];
_imagePickerVc.delegate = self;
// set appearance / 改变相册选择页的导航栏外观
_imagePickerVc.navigationBar.barTintColor = self.navigationController.navigationBar.barTintColor;
_imagePickerVc.navigationBar.tintColor = self.navigationController.navigationBar.tintColor;
UIBarButtonItem *tzBarItem, *BarItem;
if (iOS9Later) {
tzBarItem = [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[TZImagePickerController class]]];
BarItem = [UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UIImagePickerController class]]];
} else {
tzBarItem = [UIBarButtonItem appearanceWhenContainedIn:[TZImagePickerController class], nil];
BarItem = [UIBarButtonItem appearanceWhenContainedIn:[UIImagePickerController class], nil];
}
NSDictionary *titleTextAttributes = [tzBarItem titleTextAttributesForState:UIControlStateNormal];
[BarItem setTitleTextAttributes:titleTextAttributes forState:UIControlStateNormal];
}
return _imagePickerVc;
}
- (void)viewDidLoad { - (void)viewDidLoad {
[super viewDidLoad]; [super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor]; self.view.backgroundColor = [UIColor whiteColor];
...@@ -87,7 +109,13 @@ ...@@ -87,7 +109,13 @@
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == _selectedPhotos.count) { if (indexPath.row == _selectedPhotos.count) {
BOOL showSheet = self.showSheetSwitch.isOn;
if (showSheet) {
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"去相册选择", nil];
[sheet showInView:self.view];
} else {
[self pushImagePickerController]; [self pushImagePickerController];
}
} else { // preview photos or video / 预览照片或者视频 } else { // preview photos or video / 预览照片或者视频
id asset = _selectedAssets[indexPath.row]; id asset = _selectedAssets[indexPath.row];
BOOL isVideo = NO; BOOL isVideo = NO;
...@@ -130,6 +158,8 @@ ...@@ -130,6 +158,8 @@
} }
} }
#pragma mark - TZImagePickerController
- (void)pushImagePickerController { - (void)pushImagePickerController {
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9 delegate:self]; TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9 delegate:self];
...@@ -162,65 +192,90 @@ ...@@ -162,65 +192,90 @@
}]; }];
[self presentViewController:imagePickerVc animated:YES completion:nil]; [self presentViewController:imagePickerVc animated:YES completion:nil];
} }
#pragma mark Click Event #pragma mark - UIImagePickerController
- (void)deleteBtnClik:(UIButton *)sender { - (void)takePhoto {
[_selectedPhotos removeObjectAtIndex:sender.tag]; AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
[_selectedAssets removeObjectAtIndex:sender.tag]; if ((authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) && iOS8Later) {
_layout.itemCount = _selectedPhotos.count; // 无权限 做一个友好的提示
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"无法使用相机" message:@"请在iPhone的""设置-隐私-相机""中允许访问相机" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"设置", nil];
[alert show];
} else { // 调用相机
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
self.imagePickerVc.sourceType = sourceType;
if(iOS8Later) {
_imagePickerVc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
}
[self presentViewController:_imagePickerVc animated:YES completion:nil];
} else {
NSLog(@"模拟器中无法打开照相机,请在真机中使用");
}
}
}
[_collectionView performBatchUpdates:^{ - (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:sender.tag inSection:0]; [picker dismissViewControllerAnimated:YES completion:nil];
[_collectionView deleteItemsAtIndexPaths:@[indexPath]]; NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
} completion:^(BOOL finished) { if ([type isEqualToString:@"public.image"]) {
TZImagePickerController *tzImagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9 delegate:self];
tzImagePickerVc.sortAscendingByModificationDate = self.sortAscendingSwitch.isOn;
[tzImagePickerVc showProgressHUD];
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// save photo and get asset / 保存图片,获取到asset
[[TZImageManager manager] savePhotoWithImage:image completion:^{
[[TZImageManager manager] getCameraRollAlbum:NO allowPickingImage:YES completion:^(TZAlbumModel *model) {
[[TZImageManager manager] getAssetsFromFetchResult:model.result allowPickingVideo:NO allowPickingImage:YES completion:^(NSArray<TZAssetModel *> *models) {
[tzImagePickerVc hideProgressHUD];
TZAssetModel *assetModel = [models firstObject];
if (tzImagePickerVc.sortAscendingByModificationDate) {
assetModel = [models lastObject];
}
[_selectedAssets addObject:assetModel.asset];
[_selectedPhotos addObject:image];
[_collectionView reloadData]; [_collectionView reloadData];
}]; }];
} }];
}];
- (IBAction)showTakePhotoBtnSwitchClick:(UISwitch *)sender {
if (sender.isOn) {
[_showSheetSwitch setOn:NO animated:YES];
[_allowPickingImageSwitch setOn:YES animated:YES];
} }
} }
- (IBAction)showSheetSwitchClick:(UISwitch *)sender { - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
if (sender.isOn) { [picker dismissViewControllerAnimated:YES completion:nil];
[_showTakePhotoBtnSwitch setOn:NO animated:YES];
}
} }
- (IBAction)allowPickingOriginPhotoSwitchClick:(UISwitch *)sender { #pragma mark - UIActionSheetDelegate
if (sender.isOn) {
[_allowPickingImageSwitch setOn:YES animated:YES];
}
}
- (IBAction)allowPickingImageSwitchClick:(UISwitch *)sender { - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (!sender.isOn) { if (buttonIndex == 0) { // take photo / 去拍照
[_allowPickingOriginalPhotoSwitch setOn:NO animated:YES]; [self takePhoto];
[_showTakePhotoBtnSwitch setOn:NO animated:YES]; } else if (buttonIndex == 1) {
[_allowPickingVideoSwitch setOn:YES animated:YES]; [self pushImagePickerController];
} }
} }
- (IBAction)allowPickingVideoSwitchClick:(UISwitch *)sender { #pragma mark - UIAlertViewDelegate
if (!sender.isOn) {
[_allowPickingImageSwitch setOn:YES animated:YES]; - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) { // 去设置界面,开启相机访问权限
if (iOS8Later) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
} else {
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=Privacy&path=Photos"]];
}
} }
} }
#pragma mark TZImagePickerControllerDelegate #pragma mark - TZImagePickerControllerDelegate
/// User click cancel button /// User click cancel button
/// 用户点击了取消 /// 用户点击了取消
- (void)imagePickerControllerDidCancel:(TZImagePickerController *)picker { // - (void)imagePickerControllerDidCancel:(TZImagePickerController *)picker {
// NSLog(@"cancel"); // NSLog(@"cancel");
} // }
// The picker should dismiss itself; when it dismissed these handle will be called. // The picker should dismiss itself; when it dismissed these handle will be called.
// If isOriginalPhoto is YES, user picked the original photo. // If isOriginalPhoto is YES, user picked the original photo.
...@@ -236,7 +291,7 @@ ...@@ -236,7 +291,7 @@
_isSelectOriginalPhoto = isSelectOriginalPhoto; _isSelectOriginalPhoto = isSelectOriginalPhoto;
_layout.itemCount = _selectedPhotos.count; _layout.itemCount = _selectedPhotos.count;
[_collectionView reloadData]; [_collectionView reloadData];
_collectionView.contentSize = CGSizeMake(0, ((_selectedPhotos.count + 2) / 3 ) * (_margin + _itemWH)); // _collectionView.contentSize = CGSizeMake(0, ((_selectedPhotos.count + 2) / 3 ) * (_margin + _itemWH));
} }
// If user picking a video, this callback will be called. // If user picking a video, this callback will be called.
...@@ -255,7 +310,57 @@ ...@@ -255,7 +310,57 @@
// }]; // }];
[_collectionView reloadData]; [_collectionView reloadData];
_collectionView.contentSize = CGSizeMake(0, ((_selectedPhotos.count + 2) / 3 ) * (_margin + _itemWH)); // _collectionView.contentSize = CGSizeMake(0, ((_selectedPhotos.count + 2) / 3 ) * (_margin + _itemWH));
}
#pragma mark Click Event
- (void)deleteBtnClik:(UIButton *)sender {
[_selectedPhotos removeObjectAtIndex:sender.tag];
[_selectedAssets removeObjectAtIndex:sender.tag];
_layout.itemCount = _selectedPhotos.count;
[_collectionView performBatchUpdates:^{
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:sender.tag inSection:0];
[_collectionView deleteItemsAtIndexPaths:@[indexPath]];
} completion:^(BOOL finished) {
[_collectionView reloadData];
}];
}
- (IBAction)showTakePhotoBtnSwitchClick:(UISwitch *)sender {
if (sender.isOn) {
[_showSheetSwitch setOn:NO animated:YES];
[_allowPickingImageSwitch setOn:YES animated:YES];
}
}
- (IBAction)showSheetSwitchClick:(UISwitch *)sender {
if (sender.isOn) {
[_showTakePhotoBtnSwitch setOn:NO animated:YES];
[_allowPickingImageSwitch setOn:YES animated:YES];
}
}
- (IBAction)allowPickingOriginPhotoSwitchClick:(UISwitch *)sender {
if (sender.isOn) {
[_allowPickingImageSwitch setOn:YES animated:YES];
}
}
- (IBAction)allowPickingImageSwitchClick:(UISwitch *)sender {
if (!sender.isOn) {
[_allowPickingOriginalPhotoSwitch setOn:NO animated:YES];
[_showTakePhotoBtnSwitch setOn:NO animated:YES];
[_allowPickingVideoSwitch setOn:YES animated:YES];
}
}
- (IBAction)allowPickingVideoSwitchClick:(UISwitch *)sender {
if (!sender.isOn) {
[_allowPickingImageSwitch setOn:YES animated:YES];
}
} }
@end @end
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