Skip to content

Instantly share code, notes, and snippets.

@nek023
Created May 7, 2013 09:52
Show Gist options
  • Save nek023/5531520 to your computer and use it in GitHub Desktop.
Save nek023/5531520 to your computer and use it in GitHub Desktop.
UIImageView+actualImageSize
@interface UIImageView (actualImageSize)
- (CGSize)actualImageSize;
@end
#import "UIImageView+actualImageSize.h"
@implementation UIImageView (actualImageSize)
- (CGSize)actualImageSize
{
CGSize actualImageSize;
CGFloat sx = self.frame.size.width / self.image.size.width;
CGFloat sy = self.frame.size.height / self.image.size.height;
CGFloat s = 1.0;
switch (self.contentMode) {
case UIViewContentModeScaleAspectFit:
s = fminf(sx, sy);
actualImageSize = CGSizeMake(self.image.size.width * s, self.image.size.height * s);
break;
case UIViewContentModeScaleAspectFill:
s = fmaxf(sx, sy);
actualImageSize = CGSizeMake(self.image.size.width * s, self.image.size.height * s);
break;
case UIViewContentModeScaleToFill:
actualImageSize = CGSizeMake(self.image.size.width * sx, self.image.size.height * sy);
break;
default:
actualImageSize = CGSizeMake(self.image.size.width * s, self.image.size.height * s);
break;
}
return actualImageSize;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment