Skip to content

Instantly share code, notes, and snippets.

@jallen
Created May 16, 2014 21:29
Show Gist options
  • Select an option

  • Save jallen/22edd22046550a5ffebd to your computer and use it in GitHub Desktop.

Select an option

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
- (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