Skip to content

Commit 8ac9f53

Browse files
committed
Fixed up zooming precision when restoring
1 parent b7063d3 commit 8ac9f53

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

Objective-C/TOCropViewController/Views/TOCropView.m

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
2121
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2222

23-
#include <math.h>
24-
2523
#import "TOCropView.h"
2624
#import "TOCropOverlayView.h"
2725
#import "TOCropScrollView.h"
@@ -217,7 +215,7 @@ - (void)setup
217215
[self addSubview:self.foregroundContainerView];
218216

219217
self.foregroundImageView = [[UIImageView alloc] initWithImage:self.image];
220-
self.foregroundImageView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:1.0f];
218+
self.foregroundImageView.backgroundColor = [UIColor colorWithWhite:1.0f alpha:1.0f];
221219
self.foregroundImageView.layer.minificationFilter = kCAFilterTrilinear;
222220
[self.foregroundContainerView addSubview:self.foregroundImageView];
223221

@@ -322,8 +320,8 @@ - (void)layoutInitialImage
322320
//Set the crop box to the size we calculated and align in the middle of the screen
323321
CGRect frame = CGRectZero;
324322
frame.size = self.hasAspectRatio ? cropBoxSize : scaledSize;
325-
frame.origin.x = bounds.origin.x + floorf((CGRectGetWidth(bounds) - frame.size.width) * 0.5f);
326-
frame.origin.y = bounds.origin.y + floorf((CGRectGetHeight(bounds) - frame.size.height) * 0.5f);
323+
frame.origin.x = floorf(bounds.origin.x + floorf((CGRectGetWidth(bounds) - frame.size.width) * 0.5f));
324+
frame.origin.y = floorf(bounds.origin.y + floorf((CGRectGetHeight(bounds) - frame.size.height) * 0.5f));
327325
self.cropBoxFrame = frame;
328326

329327
//set the fully zoomed out state initially
@@ -418,7 +416,9 @@ - (void)matchForegroundToBackground
418416
return;
419417

420418
//We can't simply match the frames since if the images are rotated, the frame property becomes unusable
421-
self.foregroundImageView.frame = [self.backgroundContainerView.superview convertRect:self.backgroundContainerView.frame toView:self.foregroundContainerView];
419+
self.foregroundImageView.frame = [self.backgroundContainerView.superview
420+
convertRect:self.backgroundContainerView.frame
421+
toView:self.foregroundContainerView];
422422
}
423423

424424
- (void)updateCropBoxFrameWithGesturePoint:(CGPoint)point
@@ -783,20 +783,23 @@ - (void)updateToImageCropFrame:(CGRect)imageCropframe
783783

784784
// Zoom into the scroll view to the appropriate size
785785
self.scrollView.zoomScale = self.scrollView.minimumZoomScale * scale;
786-
786+
787+
CGSize contentSize = self.scrollView.contentSize;
788+
self.scrollView.contentSize = CGSizeMake(floorf(contentSize.width), floorf(contentSize.height));
789+
787790
// Work out the size and offset of the upscaled crop box
788791
CGRect frame = CGRectZero;
789-
frame.size = (CGSize){scaledCropSize.width * scale, scaledCropSize.height * scale};
792+
frame.size = (CGSize){floorf(scaledCropSize.width * scale), floorf(scaledCropSize.height * scale)};
790793

791794
//set the crop box
792795
CGRect cropBoxFrame = CGRectZero;
793796
cropBoxFrame.size = frame.size;
794-
cropBoxFrame.origin.x = CGRectGetMidX(bounds) - (frame.size.width * 0.5f);
795-
cropBoxFrame.origin.y = CGRectGetMidY(bounds) - (frame.size.height * 0.5f);
797+
cropBoxFrame.origin.x = floorf(CGRectGetMidX(bounds) - (frame.size.width * 0.5f));
798+
cropBoxFrame.origin.y = floorf(CGRectGetMidY(bounds) - (frame.size.height * 0.5f));
796799
self.cropBoxFrame = cropBoxFrame;
797800

798-
frame.origin.x = (scaledOffset.x * scale) - self.scrollView.contentInset.left;
799-
frame.origin.y = (scaledOffset.y * scale) - self.scrollView.contentInset.top;
801+
frame.origin.x = ceilf((scaledOffset.x * scale) - self.scrollView.contentInset.left);
802+
frame.origin.y = ceilf((scaledOffset.y * scale) - self.scrollView.contentInset.top);
800803
self.scrollView.contentOffset = frame.origin;
801804
}
802805

0 commit comments

Comments
 (0)