Skip to content

Commit

Permalink
1.7.4版本 加入对圆形裁剪的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
banchichen committed Dec 6, 2016
1 parent 1967aa7 commit e782989
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 40 deletions.
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.7.3"
s.version = "1.7.4"
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" => "tanzhenios@foxmail.com" }
s.platform = :ios
s.ios.deployment_target = "6.0"
s.source = { :git => "https://github.com/banchichen/TZImagePickerController.git", :tag => "1.7.3" }
s.source = { :git => "https://github.com/banchichen/TZImagePickerController.git", :tag => "1.7.4" }
s.requires_arc = true
s.resources = "TZImagePickerController/TZImagePickerController/*.{png,xib,nib,bundle}"
s.source_files = "TZImagePickerController/TZImagePickerController/*.{h,m}"
Expand Down
2 changes: 1 addition & 1 deletion TZImagePickerController/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.7.3</string>
<string>1.7.4</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@interface TZImageCropManager : NSObject

/// 裁剪框背景的处理
+ (void)overlayClippingWithView:(UIView *)view cropRect:(CGRect)cropRect containerView:(UIView *)containerView;
+ (void)overlayClippingWithView:(UIView *)view cropRect:(CGRect)cropRect containerView:(UIView *)containerView needCircleCrop:(BOOL)needCircleCrop;

/*
1.7.2 为了解决多位同学对于图片裁剪的需求,我这两天有空便在研究图片裁剪
Expand All @@ -24,4 +24,7 @@
/// 获得裁剪后的图片
+ (UIImage *)cropImageView:(UIImageView *)imageView toRect:(CGRect)rect zoomScale:(double)zoomScale containerView:(UIView *)containerView;

/// 获取圆形图片
+ (UIImage *)circularClipImage:(UIImage *)image;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,19 @@
@implementation TZImageCropManager

/// 裁剪框背景的处理
+ (void)overlayClippingWithView:(UIView *)view cropRect:(CGRect)cropRect containerView:(UIView *)containerView {
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
CGMutablePathRef path = CGPathCreateMutable();

// Left side of _cropBgView
CGPathAddRect(path, nil, CGRectMake(0, 0, cropRect.origin.x, containerView.tz_height));
// Right side of _cropBgView
CGPathAddRect(path, nil, CGRectMake(CGRectGetMaxX(cropRect), 0, view.tz_width - cropRect.origin.x - cropRect.size.width, containerView.tz_height));
// Top side of _cropBgView
CGPathAddRect(path, nil, CGRectMake(0, 0, view.tz_width, cropRect.origin.y));
// Bottom side _cropBgView
CGPathAddRect(path, nil, CGRectMake(0, CGRectGetMaxY(cropRect), view.tz_width, containerView.tz_height - cropRect.origin.y + cropRect.size.height));

maskLayer.path = path;
view.layer.mask = maskLayer;
CGPathRelease(path);
+ (void)overlayClippingWithView:(UIView *)view cropRect:(CGRect)cropRect containerView:(UIView *)containerView needCircleCrop:(BOOL)needCircleCrop {
UIBezierPath *path= [UIBezierPath bezierPathWithRect:[UIScreen mainScreen].bounds];
CAShapeLayer *layer = [CAShapeLayer layer];
if (needCircleCrop) { // 圆形裁剪框
[path appendPath:[UIBezierPath bezierPathWithArcCenter:containerView.center radius:cropRect.size.width / 2 startAngle:0 endAngle: 2 * M_PI clockwise:NO]];
} else { // 矩形裁剪框
[path appendPath:[UIBezierPath bezierPathWithRect:cropRect]];
}
layer.path = path.CGPath;
layer.fillRule = kCAFillRuleEvenOdd;
layer.fillColor = [[UIColor blackColor] CGColor];
layer.opacity = 0.5;
[view.layer addSublayer:layer];
}

/// 获得裁剪后的图片
Expand All @@ -46,7 +43,7 @@ + (UIImage *)cropImageView:(UIImageView *)imageView toRect:(CGRect)rect zoomScal
CGImageRef imageRef = [self newTransformedImage:transform
sourceImage:imageView.image.CGImage
sourceSize:imageView.image.size
outputWidth:imageView.image.size.width
outputWidth:rect.size.width * [UIScreen mainScreen].scale
cropSize:rect.size
imageViewSize:imageView.frame.size];
UIImage *cropedImage = [UIImage imageWithCGImage:imageRef];
Expand Down Expand Up @@ -95,4 +92,20 @@ + (CGImageRef)newScaledImage:(CGImageRef)source toSize:(CGSize)size {
return resultRef;
}

/// 获取圆形图片
+ (UIImage *)circularClipImage:(UIImage *)image {
UIGraphicsBeginImageContextWithOptions(image.size, NO, [UIScreen mainScreen].scale);

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
CGContextAddEllipseInRect(ctx, rect);
CGContextClip(ctx);

[image drawInRect:rect];
UIImage *circleImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();
return circleImage;
}

@end
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.7.3 - 2016.12.6
// version 1.7.4 - 2016.12.6

/*
经过测试,比起xib的方式,把TZAssetCell改用纯代码的方式来写,滑动帧数明显提高了(约提高10帧左右)
Expand Down Expand Up @@ -94,11 +94,12 @@

/// Single selection mode, valid when maxImagesCount = 1
/// 单选模式,maxImagesCount为1时才生效
@property (nonatomic, assign) BOOL showSelectBtn; ///< 在单选模式下,照片列表页中,显示选择按钮,默认为NO
@property (nonatomic, assign) BOOL allowCrop; ///< 允许裁剪,默认为YES,showSelectBtn为NO才生效
@property (nonatomic, assign) CGRect cropRect; ///< 裁剪框的尺寸
@property (nonatomic, assign) BOOL showSelectBtn; ///< 在单选模式下,照片列表页中,显示选择按钮,默认为NO
@property (nonatomic, assign) BOOL allowCrop; ///< 允许裁剪,默认为YES,showSelectBtn为NO才生效
@property (nonatomic, assign) CGRect cropRect; ///< 裁剪框的尺寸
@property (nonatomic, assign) BOOL needCircleCrop; ///< 需要圆形裁剪框
@property (nonatomic, assign) NSInteger circleCropRadius; ///< 圆形裁剪框半径大小
@property (nonatomic, copy) void (^cropViewSettingBlock)(UIView *cropView); ///< 自定义裁剪框的其他属性
@property (nonatomic, copy) void (^cropBgViewSettingBlock)(UIView *cropBgView); ///< 自定义裁剪框背景的其他属性

- (void)showAlertWithTitle:(NSString *)title;
- (void)showProgressHUD;
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.7.3 - 2016.12.6
// version 1.7.4 - 2016.12.6

#import "TZImagePickerController.h"
#import "TZPhotoPickerController.h"
Expand Down Expand Up @@ -302,6 +302,11 @@ - (void)setAllowCrop:(BOOL)allowCrop {
}
}

- (void)setCircleCropRadius:(NSInteger)circleCropRadius {
_circleCropRadius = circleCropRadius;
_cropRect = CGRectMake(self.view.tz_width / 2 - circleCropRadius, self.view.tz_height / 2 - _circleCropRadius, _circleCropRadius * 2, _circleCropRadius * 2);
}

- (CGRect)cropRect {
if (_cropRect.size.width > 0) {
return _cropRect;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,22 +183,21 @@ - (void)configCropView {
if (!_tzImagePickerVc.showSelectBtn && _tzImagePickerVc.allowCrop) {
_cropBgView = [UIView new];
_cropBgView.userInteractionEnabled = NO;
static CGFloat rgb = 34 / 255.0;
_cropBgView.backgroundColor = [UIColor colorWithRed:rgb green:rgb blue:rgb alpha:1.0];
_cropBgView.alpha = 0.5;
_cropBgView.backgroundColor = [UIColor clearColor];
_cropBgView.frame = self.view.bounds;
[self.view addSubview:_cropBgView];
if (_tzImagePickerVc.cropBgViewSettingBlock) {
_tzImagePickerVc.cropBgViewSettingBlock(_cropBgView);
}
[TZImageCropManager overlayClippingWithView:_cropBgView cropRect:_tzImagePickerVc.cropRect containerView:self.view];
[TZImageCropManager overlayClippingWithView:_cropBgView cropRect:_tzImagePickerVc.cropRect containerView:self.view needCircleCrop:_tzImagePickerVc.needCircleCrop];

_cropView = [UIView new];
_cropView.userInteractionEnabled = NO;
_cropView.backgroundColor = [UIColor clearColor];
_cropView.frame = _tzImagePickerVc.cropRect;
_cropView.layer.borderColor = [UIColor whiteColor].CGColor;
_cropView.layer.borderWidth = 1.0;
if (_tzImagePickerVc.needCircleCrop) {
_cropView.layer.cornerRadius = _tzImagePickerVc.cropRect.size.width / 2;
_cropView.clipsToBounds = YES;
}
[self.view addSubview:_cropView];
if (_tzImagePickerVc.cropViewSettingBlock) {
_tzImagePickerVc.cropViewSettingBlock(_cropView);
Expand Down Expand Up @@ -294,6 +293,9 @@ - (void)doneButtonClick {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:_currentIndex inSection:0];
TZPhotoPreviewCell *cell = (TZPhotoPreviewCell *)[_collectionView cellForItemAtIndexPath:indexPath];
UIImage *cropedImage = [TZImageCropManager cropImageView:cell.imageView toRect:_tzImagePickerVc.cropRect zoomScale:cell.scrollView.zoomScale containerView:self.view];
if (_tzImagePickerVc.needCircleCrop) {
cropedImage = [TZImageCropManager circularClipImage:cropedImage];
}
if (self.doneButtonClickBlockCropMode) {
TZAssetModel *model = _models[_currentIndex];
self.doneButtonClickBlockCropMode(cropedImage,model.asset);
Expand Down
9 changes: 3 additions & 6 deletions TZImagePickerController/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,12 @@ - (void)pushImagePickerController {
/// 5. 单选模式,maxImagesCount为1时才生效
imagePickerVc.showSelectBtn = NO;
imagePickerVc.allowCrop = YES;
// NSInteger cropWH = self.view.tz_width - 150;
// imagePickerVc.cropRect = CGRectMake(20, (self.view.tz_height - cropWH) / 2 + 50, cropWH, cropWH - 100);
imagePickerVc.needCircleCrop = YES;
imagePickerVc.circleCropRadius = 100;
/*
[imagePickerVc setCropViewSettingBlock:^(UIView *cropView) {
cropView.layer.borderColor = [UIColor redColor].CGColor;
cropView.layer.borderWidth = 2.0;
}];
/*
[imagePickerVc setCropBgViewSettingBlock:^(UIView *cropBgView) {
cropBgView.alpha = 0.5;
}];*/
#pragma mark - 到这里为止

Expand Down

0 comments on commit e782989

Please sign in to comment.