Skip to content

Commit

Permalink
可把拍照按钮放在外面了
Browse files Browse the repository at this point in the history
  • Loading branch information
banchichen committed Jun 21, 2016
1 parent b53c529 commit fd1a2b2
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 59 deletions.
Binary file modified .DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions TZImagePickerController.podspec
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
Pod::Spec.new do |s|
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.homepage = "https://github.com/banchichen/TZImagePickerController"
s.license = "MIT"
s.author = { "banchichen" => "[email protected]" }
s.platform = :ios
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.resources = "TZImagePickerController/TZImagePickerController/*.{png,xib,nib,bundle}"
s.source_files = "TZImagePickerController/TZImagePickerController/*.{h,m}"
Expand Down
4 changes: 2 additions & 2 deletions TZImagePickerController/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<rect key="frame" x="225" y="6" width="51" height="31"/>
<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>
</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"/>
<fontDescription key="fontDescription" type="system" pointSize="15"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
Expand Down
2 changes: 1 addition & 1 deletion TZImagePickerController/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.4.5</string>
<string>1.5.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//
// Created by 谭真 on 15/12/24.
// Copyright © 2015年 谭真. All rights reserved.
// version 1.4.5
// version 1.5.0

/*
经过测试,比起xib的方式,把TZAssetCell改用纯代码的方式来写,滑动帧数明显提高了(约提高10帧左右)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#import "TZImageManager.h"
#import "TZVideoPlayerController.h"

@interface TZPhotoPickerController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate> {
@interface TZPhotoPickerController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIAlertViewDelegate> {
NSMutableArray *_models;

UIButton *_previewButton;
Expand Down Expand Up @@ -357,10 +357,10 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
// take a picture / 去拍照
// take a photo / 去拍照
TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
if (((tzImagePickerVc.sortAscendingByModificationDate && indexPath.row >= _models.count) || (!tzImagePickerVc.sortAscendingByModificationDate && indexPath.row == 0)) && _showTakePhotoBtn) {
[self takePicture]; return;
[self takePhoto]; return;
}
// preview phote or video / 预览照片或视频
NSInteger index = indexPath.row;
Expand Down Expand Up @@ -395,16 +395,23 @@ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {

#pragma mark - Private Method

- (void)takePicture {
UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
self.imagePickerVc.sourceType = sourceType;
if(iOS8Later) {
_imagePickerVc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
- (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;
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
self.imagePickerVc.sourceType = sourceType;
if(iOS8Later) {
_imagePickerVc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
}
[self presentViewController:_imagePickerVc animated:YES completion:nil];
} else {
NSLog(@"模拟器中无法打开照相机,请在真机中使用");
}
[self presentViewController:_imagePickerVc animated:YES completion:nil];
} else {
NSLog(@"模拟器中无法打开照相机,请在真机中使用");
}
}

Expand Down Expand Up @@ -487,6 +494,18 @@ - (void)checkSelectedModels {
}
}

#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

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
Expand Down
187 changes: 146 additions & 41 deletions TZImagePickerController/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#import "TZImageManager.h"
#import "TZVideoPlayerController.h"

@interface ViewController ()<TZImagePickerControllerDelegate,UICollectionViewDataSource,UICollectionViewDelegate> {
@interface ViewController ()<TZImagePickerControllerDelegate,UICollectionViewDataSource,UICollectionViewDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UIAlertViewDelegate> {
NSMutableArray *_selectedPhotos;
NSMutableArray *_selectedAssets;
BOOL _isSelectOriginalPhoto;
Expand All @@ -25,6 +25,7 @@ @interface ViewController ()<TZImagePickerControllerDelegate,UICollectionViewDat
CGFloat _margin;
LxGridViewFlowLayout *_layout;
}
@property (nonatomic, strong) UIImagePickerController *imagePickerVc;
@property (nonatomic, strong) UICollectionView *collectionView;
// 6个设置开关
@property (weak, nonatomic) IBOutlet UISwitch *showTakePhotoBtnSwitch; ///< 在内部显示拍照按钮
Expand All @@ -37,6 +38,27 @@ @interface ViewController ()<TZImagePickerControllerDelegate,UICollectionViewDat

@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 {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
Expand Down Expand Up @@ -87,7 +109,13 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == _selectedPhotos.count) {
[self pushImagePickerController];
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];
}
} else { // preview photos or video / 预览照片或者视频
id asset = _selectedAssets[indexPath.row];
BOOL isVideo = NO;
Expand Down Expand Up @@ -130,6 +158,8 @@ - (void)collectionView:(UICollectionView *)collectionView itemAtIndexPath:(NSInd
}
}

#pragma mark - TZImagePickerController

- (void)pushImagePickerController {
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9 delegate:self];

Expand Down Expand Up @@ -162,65 +192,90 @@ - (void)pushImagePickerController {

}];


[self presentViewController:imagePickerVc animated:YES completion:nil];
}

#pragma mark Click Event
#pragma mark - UIImagePickerController

- (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];
- (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;
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
self.imagePickerVc.sourceType = sourceType;
if(iOS8Later) {
_imagePickerVc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
}
[self presentViewController:_imagePickerVc animated:YES completion:nil];
} else {
NSLog(@"模拟器中无法打开照相机,请在真机中使用");
}
}
}

- (IBAction)showSheetSwitchClick:(UISwitch *)sender {
if (sender.isOn) {
[_showTakePhotoBtnSwitch setOn:NO animated:YES];
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:nil];
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
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];
}];
}];
}];
}
}

- (IBAction)allowPickingOriginPhotoSwitchClick:(UISwitch *)sender {
if (sender.isOn) {
[_allowPickingImageSwitch setOn:YES animated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)allowPickingImageSwitchClick:(UISwitch *)sender {
if (!sender.isOn) {
[_allowPickingOriginalPhotoSwitch setOn:NO animated:YES];
[_showTakePhotoBtnSwitch setOn:NO animated:YES];
[_allowPickingVideoSwitch setOn:YES animated:YES];
#pragma mark - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) { // take photo / 去拍照
[self takePhoto];
} else if (buttonIndex == 1) {
[self pushImagePickerController];
}
}

- (IBAction)allowPickingVideoSwitchClick:(UISwitch *)sender {
if (!sender.isOn) {
[_allowPickingImageSwitch setOn:YES animated:YES];
#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 TZImagePickerControllerDelegate
#pragma mark - TZImagePickerControllerDelegate

/// User click cancel button
/// 用户点击了取消
- (void)imagePickerControllerDidCancel:(TZImagePickerController *)picker {
// - (void)imagePickerControllerDidCancel:(TZImagePickerController *)picker {
// NSLog(@"cancel");
}
// }

// The picker should dismiss itself; when it dismissed these handle will be called.
// If isOriginalPhoto is YES, user picked the original photo.
Expand All @@ -236,7 +291,7 @@ - (void)imagePickerController:(TZImagePickerController *)picker didFinishPicking
_isSelectOriginalPhoto = isSelectOriginalPhoto;
_layout.itemCount = _selectedPhotos.count;
[_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.
Expand All @@ -255,7 +310,57 @@ - (void)imagePickerController:(TZImagePickerController *)picker didFinishPicking

// }];
[_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

0 comments on commit fd1a2b2

Please sign in to comment.