Created
May 16, 2014 21:29
-
-
Save jallen/22edd22046550a5ffebd to your computer and use it in GitHub Desktop.
Calculate initial zoomScale and contentOffset for an ALAsset sized to fit and centered for a given screenSize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - (CGFloat)_initialZoomScaleForAsset:(ALAsset *)asset screenSize:(CGSize)screenSize { | |
| CGSize scaledSize = [AssetWorker scaledSizeForImageSize:asset.defaultRepresentation.dimensions]; | |
| CGFloat xScale = screenSize.width / scaledSize.width; // the scale needed to perfectly fit the image width-wise | |
| CGFloat yScale = screenSize.height / scaledSize.height; // the scale needed to perfectly fit the image height-wise | |
| return MAX(xScale, yScale); | |
| } | |
| - (CGPoint)_initialContentOffsetForAsset:(ALAsset *)asset screenSize:(CGSize)screenSize { | |
| CGSize scaledSize = [AssetWorker scaledSizeForImageSize:asset.defaultRepresentation.dimensions]; | |
| CGFloat zoomScale = [self _initialZoomScaleForAsset:asset screenSize:screenSize]; | |
| return CGPointMake(((scaledSize.width * zoomScale) - screenSize.width) * 0.5f, | |
| ((scaledSize.height * zoomScale) - screenSize.height) * 0.5f); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment