diff --git a/demo/ImageCropViewControllerDemo/ImageCropViewControllerDemo/ViewController.m b/demo/ImageCropViewControllerDemo/ImageCropViewControllerDemo/ViewController.m index 1f763e4..0965ab4 100644 --- a/demo/ImageCropViewControllerDemo/ImageCropViewControllerDemo/ViewController.m +++ b/demo/ImageCropViewControllerDemo/ImageCropViewControllerDemo/ViewController.m @@ -34,7 +34,6 @@ - (void)cropAndDisplayImage:(UIImage *)image picker:(UIImagePickerController *)p VFAspectRatio *aspectRatio = VFAspectRatioMake(CGRectGetWidth(imageView.frame), CGRectGetHeight(imageView.frame)); VFImageCropViewController *cropVC = [[VFImageCropViewController alloc] initWithImage:image aspectRatio:aspectRatio]; - // set crop vc properties cropVC.cropFramePadding = 60; cropVC.onCancelled = ^ { diff --git a/src/VFAspectRatio.m b/src/VFAspectRatio.m index fcf9566..3c99b51 100644 --- a/src/VFAspectRatio.m +++ b/src/VFAspectRatio.m @@ -28,21 +28,32 @@ - (instancetype)initWithWidth:(NSInteger)width height:(NSInteger)height { } - (CGSize)aspectSizeThatFits:(CGSize)size padding:(CGFloat)padding { + CGFloat cut = padding * 2; + CGSize paddedSize = CGSizeMake(size.width - cut, size.height - cut); + return [self aspectSizeThatFits:paddedSize]; +} + +- (CGSize)aspectSizeThatFits:(CGSize)size { CGFloat w = 0; CGFloat h = 0; if (_width == _height) { - w = MIN(size.width, size.height) - padding; + w = MIN(size.width, size.height); h = w; } else if (_width > _height) { - w = size.width - padding; + w = size.width; h = (w / _width) * _height; } else { - h = size.height - padding; + h = size.height; w = (h / _height) * _width; } - return CGSizeMake(w, h); + CGFloat wOverhead = MAX(1, w / size.width); + CGFloat hOverhead = MAX(1, h / size.height); + + CGFloat overhead = MAX(wOverhead, hOverhead); + + return CGSizeMake(w / overhead, h / overhead); } - (NSString *)description { diff --git a/src/VFImageCropView.m b/src/VFImageCropView.m index 29275a6..8182d3d 100644 --- a/src/VFImageCropView.m +++ b/src/VFImageCropView.m @@ -166,6 +166,8 @@ - (void)layoutSubviews { if (_scrollView.zoomScale < minimumZoomScale) { _scrollView.zoomScale = minimumZoomScale; } + + _scrollView.contentOffset = [self calculateCenterContentOffsetWithContentInset:_scrollView.contentInset]; } } @@ -180,6 +182,13 @@ - (UIEdgeInsets)calculateContentInset { return UIEdgeInsetsMake(top, leftRight, bottom, leftRight); } +- (CGPoint)calculateCenterContentOffsetWithContentInset:(UIEdgeInsets)contentInset { + CGFloat w = MAX(0, (_scrollView.contentSize.width - CGRectGetWidth(_cropAreaView.frame)) / 2); + CGFloat h = MAX(0, (_scrollView.contentSize.height - CGRectGetHeight(_cropAreaView.frame)) / 2); + + return CGPointMake(-contentInset.left + w, -contentInset.top + h); +} + #pragma mark Crop area available frame - (CGRect)availableFrameToPlaceCropArea { diff --git a/test/VFAspectRatioTests.m b/test/VFAspectRatioTests.m index 3c85143..c9d6281 100644 --- a/test/VFAspectRatioTests.m +++ b/test/VFAspectRatioTests.m @@ -40,6 +40,10 @@ + (XCTestSuite *)defaultTestSuite { [self addTestWithAspectRatio:VFAspectRatioMake(2, 1) inputSize:CGSizeMake(200, 200) expectedSize:CGSizeMake(200, 100) testSuite:testSuite]; [self addTestWithAspectRatio:VFAspectRatioMake(2, 1) inputSize:CGSizeMake(200, 100) expectedSize:CGSizeMake(200, 100) testSuite:testSuite]; + [self addTestWithAspectRatio:VFAspectRatioMake(1, 2) inputSize:CGSizeMake(50, 200) expectedSize:CGSizeMake(50, 100) testSuite:testSuite]; + + [self addTestWithAspectRatio:VFAspectRatioMake(2, 1) inputSize:CGSizeMake(200, 50) expectedSize:CGSizeMake(100, 50) testSuite:testSuite]; + return testSuite; }