Simulator Screen Shot - iPhone 8 Plus - 2018-08-09 at 09.43.43.png
.h
#import <UIKit/UIKit.h>
@interface CutImageViewControllerUNF : UIViewController
@property (nonatomic, strong) NSDictionary *imageInfoDict;
@end
.m
#import "CutImageViewControllerUNF.h"
#define kWidth ([UIScreen mainScreen].bounds.size.width * 1.0)
#define kHeight ([UIScreen mainScreen].bounds.size.height * 1.0)
#define kCropWidth (300.f)
@interface CutImageViewControllerUNF () <UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIImage *originImg;
@end
@implementation CutImageViewControllerUNF
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor blackColor]];
[self.view addSubview:self.scrollView];
[self imageview];
[self userInterface];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#pragma mark -
- (void)imageview {
CGSize imgSize = [self imageSize];
CGPoint point = CGPointMake((kWidth - kCropWidth)/2.f, (kHeight - kCropWidth)/2.f - 64.f);
UIImageView *imageview = [[UIImageView alloc] initWithImage:self.originImg];
imageview.frame = CGRectMake(point.x, point.y, imgSize.width, imgSize.height);
self.scrollView.contentSize = CGSizeMake(imgSize.width + (point.x)*2.f, imgSize.height + (point.y)*2.f);
self.scrollView.contentOffset = CGPointMake((self.scrollView.contentSize.width)/2.f - kWidth/2.0, (self.scrollView.contentSize.height)/2.f - kHeight/2.0 + 64);
[self.scrollView addSubview:imageview];
}
- (CGSize)imageSize {
CGSize imgOriginSize = self.originImg.size;
CGSize imgNewSize = CGSizeZero;
if (imgOriginSize.width/kWidth < imgOriginSize.height/kHeight) {
imgNewSize = CGSizeMake(kHeight/imgOriginSize.height*imgOriginSize.width, kHeight);
} else {
imgNewSize = CGSizeMake(kWidth, kWidth/imgOriginSize.width*imgOriginSize.height);
}
if (imgNewSize.width/kCropWidth < 1) {
imgNewSize = CGSizeMake(imgNewSize.width * (kCropWidth/imgNewSize.width), imgNewSize.height * (kCropWidth/imgNewSize.width));
}
if (imgNewSize.height/kCropWidth < 1) {
imgNewSize = CGSizeMake(imgNewSize.width * (kCropWidth/imgNewSize.height), imgNewSize.height * (kCropWidth/imgNewSize.height));
}
return imgNewSize;
}
#pragma mark -
- (void)userInterface {
CGPoint point = CGPointMake(kWidth/2.0 - 150, kHeight/2.0 - 150);
CGRect cropframe = CGRectMake(point.x, point.y, 300, 300);
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:self.view.bounds cornerRadius:0];
UIBezierPath * cropPath = [UIBezierPath bezierPathWithRoundedRect:cropframe cornerRadius:0];
if (YES) {
cropPath = [UIBezierPath bezierPathWithOvalInRect:cropframe];
}
[path appendPath:cropPath];
CAShapeLayer * layer = [[CAShapeLayer alloc] init];
layer.fillColor = [UIColor colorWithRed:.0 green:.0 blue:.0 alpha:0.5].CGColor;
//填充规则
layer.fillRule=kCAFillRuleEvenOdd;
layer.path = path.CGPath;
[self.view.layer addSublayer:layer];
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
[self centerContent];
}
- (void)centerContent {
for (UIView *imageview in self.scrollView.subviews) {
if ([imageview isKindOfClass:[UIImageView class]]) {
CGRect imageViewFrame = imageview.frame;
CGRect scrollBounds = CGRectMake(0, 0, kWidth, kHeight);
if (imageViewFrame.size.height > scrollBounds.size.height) {
imageViewFrame.origin.y = kHeight/2.f -150.f - 64.f;
}else {
imageViewFrame.origin.y = kHeight/2.f -150.f - 64.f;//(scrollBounds.size.height - imageViewFrame.size.height) / 2.0;
}
if (imageViewFrame.size.width < scrollBounds.size.width) {
imageViewFrame.origin.x = (scrollBounds.size.width - imageViewFrame.size.width) /2.f;
}else {
imageViewFrame.origin.x = kWidth/2.f - 150.f;
}
imageview.frame = imageViewFrame;
self.scrollView.contentSize = CGSizeMake(imageViewFrame.size.width + (kWidth/2.0 -150.f)*2.f, imageViewFrame.size.height + (kHeight/2.f - 150.f - 64.f)*2.f +64.f);
}
}
}
- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
for (UIView *imageview in self.scrollView.subviews) {
if ([imageview isKindOfClass:[UIImageView class]]) {
return imageview;
}
}
return nil;
}
#pragma mark - Lazying
- (UIScrollView *)scrollView {
if (!_scrollView) {
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.zoomScale = 3.0;
_scrollView.bouncesZoom = YES;
_scrollView.minimumZoomScale = 1.0;
_scrollView.maximumZoomScale = 3.0;
_scrollView.delegate = self;
}
return _scrollView;
}
- (void)setImageInfoDict:(NSDictionary *)imageInfoDict {
_imageInfoDict = imageInfoDict;
UIImage *sourceImage = [imageInfoDict objectForKey:UIImagePickerControllerOriginalImage];
self.originImg = sourceImage;
}
@end
```![Simulator Screen Shot - iPhone 8 Plus - 2018-08-09 at 09.43.43.png](https://upload-images.jianshu.io/upload_images/678464-d3abffb686a8817a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)