Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4114b14

Browse files
author
曾庆松
committed
MOD: 拆分图片预览与图片剪切功能
1 parent 5de07bd commit 4114b14

10 files changed

+57
-131
lines changed

MYImagePicker/Classes/Feature/CropImage/MYImagePickerCropImageViewController.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ NS_ASSUME_NONNULL_BEGIN
2828
@property (nonatomic, assign) NSInteger circleCropRadius;
2929
/// 是否图片等比缩放填充cropRect区域
3030
@property (nonatomic, assign) BOOL scaleAspectFillCrop;
31+
/// 裁剪回调
32+
@property (nonatomic, copy) void (^doneButtonClickBlockCropMode)(UIImage *cropedImage, id asset);
3133

3234
- (instancetype)initWithModel:(MYAsset *)model;
3335

MYImagePicker/Classes/Feature/CropImage/MYImagePickerCropImageViewController.m

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#import "MYImagePickerCropImageViewController.h"
99

10+
#import "MYAsset.h"
11+
1012
#import "MYImagePickerConfig.h"
1113

1214
#import "MYImagePickerMacro.h"
@@ -15,6 +17,8 @@
1517
#import "UIView+MYImagePickerCrop.h"
1618
#import "UIViewController+MYImagePickerHUD.h"
1719

20+
#import "MYImagePickerCropManager.h"
21+
1822
#import "MYImagePickerPhotoPreviewView.h"
1923
#import "MYImagePickerViewController.h"
2024

@@ -194,7 +198,18 @@ - (void)configCropView
194198
//MARK: - 事件响应
195199
- (void)onConfirmBtnClick:(UIButton *)sender
196200
{
201+
sender.enabled = NO;
202+
[self myp_showProgressHUD];
203+
UIImage *cropedImage = [MYImagePickerCropManager cropImageView:self.previewView.imageView toRect:self.cropRect zoomScale:self.previewView.scrollView.zoomScale containerView:self.view];
204+
if (self.needCircleCrop) {
205+
cropedImage = [MYImagePickerCropManager circularClipImage:cropedImage];
206+
}
197207

208+
sender.enabled = YES;
209+
[self myp_hideProgressHUD];
210+
if (self.doneButtonClickBlockCropMode) {
211+
self.doneButtonClickBlockCropMode(cropedImage, self.model.asset);
212+
}
198213
}
199214

200215
//MARK: - 方法重载

MYImagePicker/Classes/Feature/ImageBrowser/View/MYImagePickerAssetPreviewCell.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ NS_ASSUME_NONNULL_BEGIN
2222

2323
@property (nonatomic, strong) MYAsset *model;
2424
@property (nonatomic, assign) NSInteger index;
25-
@property (nonatomic, assign) BOOL allowCrop;
26-
@property (nonatomic, assign) CGRect cropRect;
27-
@property (nonatomic, assign) BOOL scaleAspectFillCrop;
2825

2926
- (void)updateSelectedStatus;
3027

MYImagePicker/Classes/Feature/ImageBrowser/View/MYImagePickerAssetPreviewCell.m

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ - (void)layoutSubviews {
3939

4040
- (void)configSubviews {
4141
self.previewView = [[MYImagePickerPhotoPreviewView alloc] initWithFrame:CGRectZero];
42+
self.previewView.allowCrop = NO;
4243
[self.previewView setShowSelectBtn:YES];
4344
__weak typeof(self) weakSelf = self;
4445
[self.previewView setSingleTapGestureBlock:^{
@@ -73,21 +74,6 @@ - (void)recoverSubviews {
7374
[_previewView recoverSubviews];
7475
}
7576

76-
- (void)setAllowCrop:(BOOL)allowCrop {
77-
_allowCrop = allowCrop;
78-
_previewView.allowCrop = allowCrop;
79-
}
80-
81-
- (void)setScaleAspectFillCrop:(BOOL)scaleAspectFillCrop {
82-
_scaleAspectFillCrop = scaleAspectFillCrop;
83-
_previewView.scaleAspectFillCrop = scaleAspectFillCrop;
84-
}
85-
86-
- (void)setCropRect:(CGRect)cropRect {
87-
_cropRect = cropRect;
88-
_previewView.cropRect = cropRect;
89-
}
90-
9177
- (void)setIndex:(NSInteger)index
9278
{
9379
_index = index;

MYImagePicker/Classes/Feature/ImageBrowser/ViewController/MYImagePickerPhotoPreviewViewController.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,8 @@ NS_ASSUME_NONNULL_BEGIN
2626
/// 是否返回原图
2727
@property (nonatomic, assign) BOOL isSelectOriginalPhoto;
2828

29-
/// 允许裁剪,默认为YES
30-
@property (nonatomic, assign) BOOL allowCrop;
31-
// 裁剪框的尺寸
32-
@property (nonatomic, assign) CGRect cropRect;
33-
/// 需要圆角裁剪框
34-
@property (nonatomic, assign) BOOL needCircleCrop;
35-
/// 圆角裁剪框半径大小
36-
@property (nonatomic, assign) NSInteger circleCropRadius;
37-
/// 是否图片等比缩放填充cropRect区域
38-
@property (nonatomic, assign) BOOL scaleAspectFillCrop;
39-
4029
/// 返回最新的选中图片数组
4130
@property (nonatomic, copy) void (^backButtonClickBlock)(BOOL isSelectOriginalPhoto);
42-
/// 裁剪回调
43-
@property (nonatomic, copy) void (^doneButtonClickBlockCropMode)(UIImage *cropedImage, id asset);
44-
4531

4632
@end
4733

MYImagePicker/Classes/Feature/ImageBrowser/ViewController/MYImagePickerPhotoPreviewViewController.m

Lines changed: 3 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ @interface MYImagePickerPhotoPreviewViewController ()<UICollectionViewDelegate,
4343

4444
@implementation MYImagePickerPhotoPreviewViewController
4545

46-
@synthesize cropRect = _cropRect;
47-
4846
//MARK: - 生命周期
4947
- (instancetype)init
5048
{
@@ -78,8 +76,6 @@ - (void)viewDidLayoutSubviews
7876
_flowLayout.minimumLineSpacing = 0;
7977
_collectionView.frame = CGRectMake(-10, MY_IMG_Navigation_H, self.view.myp_width + 20, height);
8078
[_collectionView setCollectionViewLayout:_flowLayout];
81-
82-
[self configCropView];
8379
}
8480

8581
//MARK: - getter && setter
@@ -146,54 +142,13 @@ - (UIView *)cropBgView
146142
return _cropBgView;
147143
}
148144

149-
- (CGRect)cropRect
150-
{
151-
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
152-
BOOL isFullScreen = self.view.myp_height == screenHeight;
153-
if (isFullScreen) {
154-
return _cropRect;
155-
} else {
156-
CGRect newCropRect = _cropRect;
157-
newCropRect.origin.y -= ((screenHeight - self.view.myp_height) / 2);
158-
return newCropRect;
159-
}
160-
}
161-
162145
//MARK: - 视图更新
163146
- (void)setupUI
164147
{
165148
[self.view addSubview:self.naviBar];
166149
[self.view addSubview:self.collectionView];
167150
}
168151

169-
- (void)configCropView
170-
{
171-
MYImagePickerConfig *config = self.imagePickerVC.config;
172-
if (config.maxImagesCount <= 1 && config.allowCrop) {
173-
if (self.circleCropRadius > 0) {
174-
self.cropRect = CGRectMake(self.view.myp_width / 2 - _circleCropRadius, self.view.myp_height / 2 - _circleCropRadius, _circleCropRadius * 2, _circleCropRadius * 2);
175-
}
176-
177-
[self.cropView removeFromSuperview];
178-
[self.cropBgView removeFromSuperview];
179-
180-
[self.cropBgView setFrame:self.view.bounds];
181-
[self.view addSubview:self.cropBgView];
182-
[UIView my_overlayClippingWithView:self.cropBgView
183-
cropRect:self.cropRect
184-
containerView:self.view
185-
needCircleCrop:self.needCircleCrop];
186-
187-
[self.cropView setFrame:self.cropRect];
188-
[self.view addSubview:self.cropView];
189-
if (self.needCircleCrop) {
190-
self.cropView.layer.cornerRadius = self.cropRect.size.width / 2;
191-
[self.cropView setClipsToBounds:YES];
192-
}
193-
194-
[self.view bringSubviewToFront:self.naviBar];
195-
}
196-
}
197152

198153
//MARK: - UIScrollViewDelegate
199154
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
@@ -222,9 +177,6 @@ - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collection
222177
{
223178
MYAsset *model = [self.models objectAtIndex:indexPath.item];
224179
MYImagePickerAssetPreviewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MYImagePickerPreviewPohotoReuseCellIdentifier forIndexPath:indexPath];
225-
cell.allowCrop = self.allowCrop;
226-
cell.cropRect = self.cropRect;
227-
cell.scaleAspectFillCrop = self.scaleAspectFillCrop;
228180
[cell setIndex:[self.imagePickerVC.selectedAssetIds indexOfObject:model.asset.localIdentifier] + 1];
229181
__weak typeof(_imagePickerVC) weakImagePickerVC = _imagePickerVC;
230182
__weak typeof(cell) weakCell = cell;
@@ -279,13 +231,8 @@ - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collection
279231
//MARK: - 私有方法
280232
- (void)refershAssetSelectedStatus
281233
{
282-
if (self.allowCrop && self.imagePickerVC.config.maxImagesCount <= 1) {
283-
[self.naviBar.nextButton setHidden:NO];
284-
[self.naviBar.nextButton setTitle:@"下一步" forState:UIControlStateNormal];
285-
} else {
286-
self.naviBar.nextButton.hidden = self.imagePickerVC.selectedModels.count <= 0;
287-
[self.naviBar.nextButton setTitle:[NSString stringWithFormat:@"下一步(%zd)", self.imagePickerVC.selectedModels.count] forState:UIControlStateNormal];
288-
}
234+
self.naviBar.nextButton.hidden = self.imagePickerVC.selectedModels.count <= 0;
235+
[self.naviBar.nextButton setTitle:[NSString stringWithFormat:@"下一步(%zd)", self.imagePickerVC.selectedModels.count] forState:UIControlStateNormal];
289236
}
290237

291238
//MARK: - 弹窗
@@ -309,27 +256,8 @@ - (void)onBackButtonClick
309256

310257
- (void)onDoneButtonClick:(UIButton *)sender
311258
{
312-
//图片裁剪
313-
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:self.currentIndex inSection:0];
314-
MYImagePickerAssetPreviewCell *cell = (MYImagePickerAssetPreviewCell *)[_collectionView cellForItemAtIndexPath:indexPath];
315-
if (self.allowCrop && [cell isKindOfClass:[MYImagePickerAssetPreviewCell class]]) {
259+
if ([self.imagePickerVC handleDoneButtonClick]) {
316260
sender.enabled = NO;
317-
[self myp_showProgressHUD];
318-
UIImage *cropedImage = [MYImagePickerCropManager cropImageView:cell.previewView.imageView toRect:self.cropRect zoomScale:cell.previewView.scrollView.zoomScale containerView:self.view];
319-
if (self.needCircleCrop) {
320-
cropedImage = [MYImagePickerCropManager circularClipImage:cropedImage];
321-
}
322-
323-
sender.enabled = YES;
324-
[self myp_hideProgressHUD];
325-
if (self.doneButtonClickBlockCropMode) {
326-
MYAsset *model = self.models[self.currentIndex];
327-
self.doneButtonClickBlockCropMode(cropedImage, model.asset);
328-
}
329-
} else {
330-
if ([self.imagePickerVC handleDoneButtonClick]) {
331-
sender.enabled = NO;
332-
}
333261
}
334262
}
335263

MYImagePicker/Classes/Feature/ImagePicker/Asset/MYImagePickerAssetViewController.m

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPa
215215
{
216216
NSInteger index = indexPath.item;
217217
if (self.assetPickerVC.config.allowCrop) {
218+
__weak typeof(self) weakSelf = self;
218219
MYAsset *model = [self.albumModel.models objectAtIndex:index];
219220
MYImagePickerCropImageViewController *cropImageVC = [[MYImagePickerCropImageViewController alloc] initWithModel:model];
220221
[cropImageVC setImagePickerVC:self.assetPickerVC];
@@ -223,6 +224,10 @@ - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPa
223224
[cropImageVC setNeedCircleCrop:self.assetPickerVC.config.needCircleCrop];
224225
[cropImageVC setAllowCrop:self.assetPickerVC.config.allowCrop];
225226
[cropImageVC setScaleAspectFillCrop:self.assetPickerVC.config.scaleAspectFillCrop];
227+
[cropImageVC setDoneButtonClickBlockCropMode:^(UIImage * cropedImage, id asset) {
228+
__strong typeof(weakSelf) strongSelf = weakSelf;
229+
[strongSelf handlerOnCropImage:cropedImage asset:asset];
230+
}];
226231
[self.navigationController pushViewController:cropImageVC animated:YES];
227232
} else {
228233
MYImagePickerPhotoPreviewViewController *previewVC = [[MYImagePickerPhotoPreviewViewController alloc] init];
@@ -237,12 +242,6 @@ - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPa
237242
[strongSelf.collectionView reloadData];
238243
[strongSelf.assetPickerVC refershAssetSelectedStatus];
239244
}];
240-
241-
[previewVC setDoneButtonClickBlockCropMode:^(UIImage * cropedImage, id asset) {
242-
__strong typeof(weakSelf) strongSelf = weakSelf;
243-
[strongSelf handlerOnCropImage:cropedImage asset:asset];
244-
}];
245-
246245
[self.navigationController pushViewController:previewVC animated:YES];
247246
}
248247
}

MYImagePicker/Classes/Feature/ImagePicker/Video/MYImagePickerVideoViewController.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ NS_ASSUME_NONNULL_BEGIN
1616

1717
@property (nonatomic, weak) MYImagePickerViewController *assetPickerVC;
1818

19+
- (void)reload;
20+
1921
@end
2022

2123
NS_ASSUME_NONNULL_END

MYImagePicker/Classes/Feature/ImagePicker/Video/MYImagePickerVideoViewController.m

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#import "MYImagePickerManager.h"
2828
#import "MYImagePickerManager+Queue.h"
29+
#import "MYImagePickerManager+Authorization.h"
2930

3031
static NSString *const MYImagePickerVideoCellReuseIdentifier = @"MYImagePickerVideoCellReuseIdentifier";
3132

@@ -105,30 +106,39 @@ - (void)setupUI
105106
}];
106107
}
107108

109+
//MARK: - 公开方法
110+
- (void)reload
111+
{
112+
[self handlerUpdateAbumModels];
113+
}
114+
115+
//MARK: - 私有方法
108116
- (void)handlerUpdateAbumModels
109117
{
110-
dispatch_async(dispatch_get_global_queue(0, 0), ^{
111-
@autoreleasepool {
112-
[[MYImagePickerManager shared] getAllAlbums:YES allowPickingImage:NO needFetchAssets:YES completion:^(NSArray<MYAlbum *> * _Nonnull models) {
113-
if ([models count] > 0) {
114-
self->_albumModels = models;
115-
for (MYAlbum *album in self->_albumModels) {
116-
if ([album.models count] > 0) {
117-
[self.assetModels addObjectsFromArray:album.models];
118+
if ([[MYImagePickerManager shared] authorizationStatusAuthorized]) {
119+
dispatch_async(dispatch_get_global_queue(0, 0), ^{
120+
@autoreleasepool {
121+
[[MYImagePickerManager shared] getAllAlbums:YES allowPickingImage:NO needFetchAssets:YES completion:^(NSArray<MYAlbum *> * _Nonnull models) {
122+
if ([models count] > 0) {
123+
self->_albumModels = models;
124+
for (MYAlbum *album in self->_albumModels) {
125+
if ([album.models count] > 0) {
126+
[self.assetModels addObjectsFromArray:album.models];
127+
}
118128
}
119129
}
120-
}
121-
}];
122-
}
123-
124-
dispatch_async(dispatch_get_main_queue(), ^{
125-
if ([self.assetModels count] > 0) {
126-
[self.collectionView reloadData];
127-
} else {
128-
[self.tipsLabel setHidden:NO];
130+
}];
129131
}
132+
133+
dispatch_async(dispatch_get_main_queue(), ^{
134+
if ([self.assetModels count] > 0) {
135+
[self.collectionView reloadData];
136+
} else {
137+
[self.tipsLabel setHidden:NO];
138+
}
139+
});
130140
});
131-
});
141+
}
132142
}
133143

134144
//MARK: - UICollectionViewDelegate && UICollectionViewDataSource

MYImagePicker/Classes/Feature/ImagePicker/ViewController/MYImagePickerViewController.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,7 @@ - (void)observeAuthrizationStatusChange {
408408
if ([[MYImagePickerManager shared] authorizationStatusAuthorized]) {
409409
[self.authorizationTipsView removeFromSuperview];
410410
[self handlerFetchAlbumData];
411+
[self.videoChildVC reload];
411412
}
412413
}
413414

0 commit comments

Comments
 (0)