Commit e7829894 authored by banchichen's avatar banchichen

1.7.4版本 加入对圆形裁剪的支持

parent 1967aa71
Pod::Spec.new do |s| Pod::Spec.new do |s|
s.name = "TZImagePickerController" 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.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" => "tanzhenios@foxmail.com" } s.author = { "banchichen" => "tanzhenios@foxmail.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.7.3" } s.source = { :git => "https://github.com/banchichen/TZImagePickerController.git", :tag => "1.7.4" }
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}"
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
<key>CFBundlePackageType</key> <key>CFBundlePackageType</key>
<string>APPL</string> <string>APPL</string>
<key>CFBundleShortVersionString</key> <key>CFBundleShortVersionString</key>
<string>1.7.3</string> <string>1.7.4</string>
<key>CFBundleSignature</key> <key>CFBundleSignature</key>
<string>????</string> <string>????</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
@interface TZImageCropManager : NSObject @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 为了解决多位同学对于图片裁剪的需求,我这两天有空便在研究图片裁剪 1.7.2 为了解决多位同学对于图片裁剪的需求,我这两天有空便在研究图片裁剪
...@@ -24,4 +24,7 @@ ...@@ -24,4 +24,7 @@
/// 获得裁剪后的图片 /// 获得裁剪后的图片
+ (UIImage *)cropImageView:(UIImageView *)imageView toRect:(CGRect)rect zoomScale:(double)zoomScale containerView:(UIView *)containerView; + (UIImage *)cropImageView:(UIImageView *)imageView toRect:(CGRect)rect zoomScale:(double)zoomScale containerView:(UIView *)containerView;
/// 获取圆形图片
+ (UIImage *)circularClipImage:(UIImage *)image;
@end @end
...@@ -12,22 +12,19 @@ ...@@ -12,22 +12,19 @@
@implementation TZImageCropManager @implementation TZImageCropManager
/// 裁剪框背景的处理 /// 裁剪框背景的处理
+ (void)overlayClippingWithView:(UIView *)view cropRect:(CGRect)cropRect containerView:(UIView *)containerView { + (void)overlayClippingWithView:(UIView *)view cropRect:(CGRect)cropRect containerView:(UIView *)containerView needCircleCrop:(BOOL)needCircleCrop {
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init]; UIBezierPath *path= [UIBezierPath bezierPathWithRect:[UIScreen mainScreen].bounds];
CGMutablePathRef path = CGPathCreateMutable(); CAShapeLayer *layer = [CAShapeLayer layer];
if (needCircleCrop) { // 圆形裁剪框
// Left side of _cropBgView [path appendPath:[UIBezierPath bezierPathWithArcCenter:containerView.center radius:cropRect.size.width / 2 startAngle:0 endAngle: 2 * M_PI clockwise:NO]];
CGPathAddRect(path, nil, CGRectMake(0, 0, cropRect.origin.x, containerView.tz_height)); } else { // 矩形裁剪框
// Right side of _cropBgView [path appendPath:[UIBezierPath bezierPathWithRect:cropRect]];
CGPathAddRect(path, nil, CGRectMake(CGRectGetMaxX(cropRect), 0, view.tz_width - cropRect.origin.x - cropRect.size.width, containerView.tz_height)); }
// Top side of _cropBgView layer.path = path.CGPath;
CGPathAddRect(path, nil, CGRectMake(0, 0, view.tz_width, cropRect.origin.y)); layer.fillRule = kCAFillRuleEvenOdd;
// Bottom side _cropBgView layer.fillColor = [[UIColor blackColor] CGColor];
CGPathAddRect(path, nil, CGRectMake(0, CGRectGetMaxY(cropRect), view.tz_width, containerView.tz_height - cropRect.origin.y + cropRect.size.height)); layer.opacity = 0.5;
[view.layer addSublayer:layer];
maskLayer.path = path;
view.layer.mask = maskLayer;
CGPathRelease(path);
} }
/// 获得裁剪后的图片 /// 获得裁剪后的图片
...@@ -46,7 +43,7 @@ ...@@ -46,7 +43,7 @@
CGImageRef imageRef = [self newTransformedImage:transform CGImageRef imageRef = [self newTransformedImage:transform
sourceImage:imageView.image.CGImage sourceImage:imageView.image.CGImage
sourceSize:imageView.image.size sourceSize:imageView.image.size
outputWidth:imageView.image.size.width outputWidth:rect.size.width * [UIScreen mainScreen].scale
cropSize:rect.size cropSize:rect.size
imageViewSize:imageView.frame.size]; imageViewSize:imageView.frame.size];
UIImage *cropedImage = [UIImage imageWithCGImage:imageRef]; UIImage *cropedImage = [UIImage imageWithCGImage:imageRef];
...@@ -95,4 +92,20 @@ ...@@ -95,4 +92,20 @@
return resultRef; 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 @end
...@@ -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.7.3 - 2016.12.6 // version 1.7.4 - 2016.12.6
/* /*
经过测试,比起xib的方式,把TZAssetCell改用纯代码的方式来写,滑动帧数明显提高了(约提高10帧左右) 经过测试,比起xib的方式,把TZAssetCell改用纯代码的方式来写,滑动帧数明显提高了(约提高10帧左右)
...@@ -94,11 +94,12 @@ ...@@ -94,11 +94,12 @@
/// Single selection mode, valid when maxImagesCount = 1 /// Single selection mode, valid when maxImagesCount = 1
/// 单选模式,maxImagesCount为1时才生效 /// 单选模式,maxImagesCount为1时才生效
@property (nonatomic, assign) BOOL showSelectBtn; ///< 在单选模式下,照片列表页中,显示选择按钮,默认为NO @property (nonatomic, assign) BOOL showSelectBtn; ///< 在单选模式下,照片列表页中,显示选择按钮,默认为NO
@property (nonatomic, assign) BOOL allowCrop; ///< 允许裁剪,默认为YES,showSelectBtn为NO才生效 @property (nonatomic, assign) BOOL allowCrop; ///< 允许裁剪,默认为YES,showSelectBtn为NO才生效
@property (nonatomic, assign) CGRect cropRect; ///< 裁剪框的尺寸 @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 (^cropViewSettingBlock)(UIView *cropView); ///< 自定义裁剪框的其他属性
@property (nonatomic, copy) void (^cropBgViewSettingBlock)(UIView *cropBgView); ///< 自定义裁剪框背景的其他属性
- (void)showAlertWithTitle:(NSString *)title; - (void)showAlertWithTitle:(NSString *)title;
- (void)showProgressHUD; - (void)showProgressHUD;
......
...@@ -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.7.3 - 2016.12.6 // version 1.7.4 - 2016.12.6
#import "TZImagePickerController.h" #import "TZImagePickerController.h"
#import "TZPhotoPickerController.h" #import "TZPhotoPickerController.h"
...@@ -302,6 +302,11 @@ ...@@ -302,6 +302,11 @@
} }
} }
- (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 { - (CGRect)cropRect {
if (_cropRect.size.width > 0) { if (_cropRect.size.width > 0) {
return _cropRect; return _cropRect;
......
...@@ -183,15 +183,10 @@ ...@@ -183,15 +183,10 @@
if (!_tzImagePickerVc.showSelectBtn && _tzImagePickerVc.allowCrop) { if (!_tzImagePickerVc.showSelectBtn && _tzImagePickerVc.allowCrop) {
_cropBgView = [UIView new]; _cropBgView = [UIView new];
_cropBgView.userInteractionEnabled = NO; _cropBgView.userInteractionEnabled = NO;
static CGFloat rgb = 34 / 255.0; _cropBgView.backgroundColor = [UIColor clearColor];
_cropBgView.backgroundColor = [UIColor colorWithRed:rgb green:rgb blue:rgb alpha:1.0];
_cropBgView.alpha = 0.5;
_cropBgView.frame = self.view.bounds; _cropBgView.frame = self.view.bounds;
[self.view addSubview:_cropBgView]; [self.view addSubview:_cropBgView];
if (_tzImagePickerVc.cropBgViewSettingBlock) { [TZImageCropManager overlayClippingWithView:_cropBgView cropRect:_tzImagePickerVc.cropRect containerView:self.view needCircleCrop:_tzImagePickerVc.needCircleCrop];
_tzImagePickerVc.cropBgViewSettingBlock(_cropBgView);
}
[TZImageCropManager overlayClippingWithView:_cropBgView cropRect:_tzImagePickerVc.cropRect containerView:self.view];
_cropView = [UIView new]; _cropView = [UIView new];
_cropView.userInteractionEnabled = NO; _cropView.userInteractionEnabled = NO;
...@@ -199,6 +194,10 @@ ...@@ -199,6 +194,10 @@
_cropView.frame = _tzImagePickerVc.cropRect; _cropView.frame = _tzImagePickerVc.cropRect;
_cropView.layer.borderColor = [UIColor whiteColor].CGColor; _cropView.layer.borderColor = [UIColor whiteColor].CGColor;
_cropView.layer.borderWidth = 1.0; _cropView.layer.borderWidth = 1.0;
if (_tzImagePickerVc.needCircleCrop) {
_cropView.layer.cornerRadius = _tzImagePickerVc.cropRect.size.width / 2;
_cropView.clipsToBounds = YES;
}
[self.view addSubview:_cropView]; [self.view addSubview:_cropView];
if (_tzImagePickerVc.cropViewSettingBlock) { if (_tzImagePickerVc.cropViewSettingBlock) {
_tzImagePickerVc.cropViewSettingBlock(_cropView); _tzImagePickerVc.cropViewSettingBlock(_cropView);
...@@ -294,6 +293,9 @@ ...@@ -294,6 +293,9 @@
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:_currentIndex inSection:0]; NSIndexPath *indexPath = [NSIndexPath indexPathForItem:_currentIndex inSection:0];
TZPhotoPreviewCell *cell = (TZPhotoPreviewCell *)[_collectionView cellForItemAtIndexPath:indexPath]; TZPhotoPreviewCell *cell = (TZPhotoPreviewCell *)[_collectionView cellForItemAtIndexPath:indexPath];
UIImage *cropedImage = [TZImageCropManager cropImageView:cell.imageView toRect:_tzImagePickerVc.cropRect zoomScale:cell.scrollView.zoomScale containerView:self.view]; 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) { if (self.doneButtonClickBlockCropMode) {
TZAssetModel *model = _models[_currentIndex]; TZAssetModel *model = _models[_currentIndex];
self.doneButtonClickBlockCropMode(cropedImage,model.asset); self.doneButtonClickBlockCropMode(cropedImage,model.asset);
......
...@@ -223,15 +223,12 @@ ...@@ -223,15 +223,12 @@
/// 5. 单选模式,maxImagesCount为1时才生效 /// 5. 单选模式,maxImagesCount为1时才生效
imagePickerVc.showSelectBtn = NO; imagePickerVc.showSelectBtn = NO;
imagePickerVc.allowCrop = YES; imagePickerVc.allowCrop = YES;
// NSInteger cropWH = self.view.tz_width - 150; imagePickerVc.needCircleCrop = YES;
// imagePickerVc.cropRect = CGRectMake(20, (self.view.tz_height - cropWH) / 2 + 50, cropWH, cropWH - 100); imagePickerVc.circleCropRadius = 100;
/*
[imagePickerVc setCropViewSettingBlock:^(UIView *cropView) { [imagePickerVc setCropViewSettingBlock:^(UIView *cropView) {
cropView.layer.borderColor = [UIColor redColor].CGColor; cropView.layer.borderColor = [UIColor redColor].CGColor;
cropView.layer.borderWidth = 2.0; cropView.layer.borderWidth = 2.0;
}];
/*
[imagePickerVc setCropBgViewSettingBlock:^(UIView *cropBgView) {
cropBgView.alpha = 0.5;
}];*/ }];*/
#pragma mark - 到这里为止 #pragma mark - 到这里为止
......
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