我寫一個UIImageView類別爲:
的UIImageView + ContentScale.h
#import <Foundation/Foundation.h>
@interface UIImageView (UIImageView_ContentScale)
-(CGFloat)contentScaleFactor;
@end
的UIImageView + ContentScale.m
#import "UIImageView+ContentScale.h"
@implementation UIImageView (UIImageView_ContentScale)
-(CGFloat)contentScaleFactor
{
CGFloat widthScale = self.bounds.size.width/self.image.size.width;
CGFloat heightScale = self.bounds.size.height/self.image.size.height;
if (self.contentMode == UIViewContentModeScaleToFill) {
return (widthScale==heightScale) ? widthScale : NAN;
}
if (self.contentMode == UIViewContentModeScaleAspectFit) {
return MIN(widthScale, heightScale);
}
if (self.contentMode == UIViewContentModeScaleAspectFill) {
return MAX(widthScale, heightScale);
}
return 1.0;
}
@end
我最後計算整個我需要的東西,而不是與aspectFit內容模式使用UIImageView。很方便,並且丟失了許多詳細信息。 – AechoLiu