Skip to content

Instantly share code, notes, and snippets.

@wndxlori
Created August 12, 2009 20:21
Show Gist options
  • Save wndxlori/166732 to your computer and use it in GitHub Desktop.
Save wndxlori/166732 to your computer and use it in GitHub Desktop.
// From:
// http://www.stone.com/The_Cocoa_Files/Thanks_A_Bundle_.html
// here's how we determine the size of the actual image in the ImageView:
// I reused this code from PhotoToWeb, since it worked already!
- (float)photoReduction {
NSSize size = [[self image] size];
NSRect iFrame = [self bounds];
if (NSWidth(iFrame) > size.width && NSHeight(iFrame) > size.height) return 1.0; // it fits
else {
// one leg of the photo doesn't fit - the smallest ratio rules
double xRatio = NSWidth(iFrame)/size.width;
double yRatio = NSHeight(iFrame)/size.height;
return MIN (xRatio, yRatio);
}
}
- (NSRect)photoRectInImageView {
NSSize size = [[self image] size];
NSRect iBounds = [self bounds];
float reduction = [self photoReduction];
NSRect photoRect;
photoRect.size.width = floor(size.width * reduction + 0.5);
photoRect.size.height = floor(size.height * reduction + 0.5);
photoRect.origin.x = floor((iBounds.size.width - photoRect.size.width)/2.0 + 0.5);
photoRect.origin.y = floor((iBounds.size.height - photoRect.size.height)/2.0 + 0.5);
return (photoRect);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment