Skip to content

Commit

Permalink
Merge pull request #2 from vlfm/dev-2.1.1
Browse files Browse the repository at this point in the history
v2.1.1
  • Loading branch information
vlfm committed Sep 27, 2014
2 parents 81b3730 + 236c835 commit 6d325af
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ^ {
Expand Down
19 changes: 15 additions & 4 deletions src/VFAspectRatio.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions src/VFImageCropView.m
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ - (void)layoutSubviews {
if (_scrollView.zoomScale < minimumZoomScale) {
_scrollView.zoomScale = minimumZoomScale;
}

_scrollView.contentOffset = [self calculateCenterContentOffsetWithContentInset:_scrollView.contentInset];
}
}

Expand All @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions test/VFAspectRatioTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 6d325af

Please sign in to comment.