2013-03-16 95 views
3

我有一些UIImage并将其显示在UIImageView中。UIImageView中的UIImage大小

我需要使用UIViewContentModeScaleAspectFit内容模式。

下面是简单的代码:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds]; 
imageView.image = self.image; 
imageView.contentMode = UIViewContentModeScaleAspectFit; 
[self.view addSubview:imageView]; 
[imageView release]; 

如何知道哪些是显示在屏幕上的图像的大小?有没有解决方案,属性或我应该手动计算?

编辑:

我知道属性image.size

self.image.size - >>>原始图像的大小

imageView.image.size - >>>大小的ImageView的,但不能显示图像。

我问的显示尺寸取决于imageView's size和它的contentmode。它不可能。

+1

http://stackoverflow.com/questions/6856879/iphone-getting-the-size-of-an-image-after-aspectft – iPatel 2013-03-16 10:12:04

回答

5

这里有UIImageView一个类别,你可以用它来反思基础上,UIViewContentMode集上的图像视图所显示的图像的边界:

@implementation UIImageView (JRAdditions) 

- (CGRect)displayedImageBounds { 
    UIImage *image = [self image]; 
    if(self.contentMode != UIViewContentModeScaleAspectFit || !image) 
     return CGRectInfinite; 

    CGFloat boundsWidth = [self bounds].size.width, 
      boundsHeight = [self bounds].size.height; 

    CGSize imageSize = [image size]; 
    CGFloat imageRatio = imageSize.width/imageSize.height; 
    CGFloat viewRatio = boundsWidth/boundsHeight; 

    if(imageRatio < viewRatio) { 
     CGFloat scale = boundsHeight/imageSize.height; 
     CGFloat width = scale * imageSize.width; 
     CGFloat topLeftX = (boundsWidth - width) * 0.5; 
     return CGRectMake(topLeftX, 0, width, boundsHeight); 
    } 

    CGFloat scale = boundsWidth/imageSize.width; 
    CGFloat height = scale * imageSize.height; 
    CGFloat topLeftY = (boundsHeight - height) * 0.5; 

    return CGRectMake(0, topLeftY, boundsWidth, height); 
} 

@end 
+0

非常平易近人的方法。 适合我! – AnkitRox 2014-09-17 11:15:23

0

它是不可能的。您可以通过以下方法

UIImage *image = imageView.image; 
CGSize size = image.size; 

大小

图像的尺寸,拍摄定向兼顾得到原始图像的大小。 (只读)

@property(nonatomic, readonly) CGSize size 
0

你也可以得到它通过使用以下方法:

-(CGSize)imageSizeAfterAspectFit:(UIImageView*)imgview{ 


float newwidth; 
float newheight; 

UIImage *image=imgview.image; 

if (image.size.height>=image.size.width){ 
    newheight=imgview.frame.size.height; 
    newwidth=(image.size.width/image.size.height)*newheight; 

    if(newwidth>imgview.frame.size.width){ 
     float diff=imgview.frame.size.width-newwidth; 
     newheight=newheight+diff/newheight*newheight; 
     newwidth=imgview.frame.size.width; 
    } 

} 
else{ 
    newwidth=imgview.frame.size.width; 
    newheight=(image.size.height/image.size.width)*newwidth; 

    if(newheight>imgview.frame.size.height){ 
     float diff=imgview.frame.size.height-newheight; 
     newwidth=newwidth+diff/newwidth*newwidth; 
     newheight=imgview.frame.size.height; 
    } 
} 

NSLog(@"image after aspect fit: width=%f height=%f",newwidth,newheight); 

return CGSizeMake(newwidth, newheight); 

} 
3

在迅速,相同@雅各布-relkin

extension UIImageView { 

    func displayedImageBounds() -> CGRect { 

     let boundsWidth = bounds.size.width 
     let boundsHeight = bounds.size.height 
     let imageSize = image!.size 
     let imageRatio = imageSize.width/imageSize.height 
     let viewRatio = boundsWidth/boundsHeight 
     if (viewRatio > imageRatio) { 
      let scale = boundsHeight/imageSize.height 
      let width = scale * imageSize.width 
      let topLeftX = (boundsWidth - width) * 0.5 
      return CGRectMake(topLeftX, 0, width, boundsHeight) 
     } 
     let scale = boundsWidth/imageSize.width 
     let height = scale * imageSize.height 
     let topLeftY = (boundsHeight - height) * 0.5 
     return CGRectMake(0,topLeftY, boundsWidth,height) 
    } 
}