2013-03-16 5 views
3

У меня есть UIImage и отобразите его в UIImageView.Размер UIImage в UIImageView

Мне нужно использовать режим содержимого UIViewContentModeScaleAspectFit.

Вот простой код:

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

Как узнать размер изображения, который отображается на экране? Есть ли какие-либо решения, свойства или я должен вычислить их вручную?

EDIT:

Я знаю о собственности 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

ответ

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

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

В быстры, такой же, как @ Яаков-Релкин

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) 
    } 
} 
Смежные вопросы