2012-05-17 94 views
0

我用户界面生成器来布局我的视图和图像视图。我在视图上放置了一个imageView并将其设置为出口。问题是,不管是什么尺寸图像(768 * 1024,1024 * 700)我在程序中设置:如何根据图像的大小动态设置imageView的大小

self.imageView.image = [UIImage imageNamed:self.filename]; 

的尺寸在屏幕上始终是我在设置ImageView的大小界面生成器。如何根据图像的大小动态设置imageView的大小?谢谢!

回答

4

喜欢的东西:

UIImageView *imageView = [[UIImageView alloc] init]; 

UIImage *image = [UIImage imageNamed:@"image.png"]; 
CGSize imageSize = [image size]; 

imageView.frame = CGRectMake(0, 0, imageSize.width, imageSize.height); 
1
UIImage *image = [UIImage imageNamed:self.filename]; 
CGFloat width = image.size.width; 
CGFloat height = image.size.height; 
self.imageView.frame = CGRectMake(x, y , width, height); 
0
UIImage *buttonImage2=[UIImage imageNamed:@"blank_button_blue.png"]; 
oButton=[[UIButton alloc] init]; 
// [oButton setImage:buttonImage2 forState:UIControlStateNormal]; 
[oButton setFrame:CGRectMake(0, 0, buttonImage2.size.width, buttonImage2.size.height)]; 
0
UIImage* sourceImage = [UIImage imageNamed:self.filename];; 

CGRect rect; 
rect.size.width = CGImageGetWidth(sourceImage.CGImage); 
rect.size.height = CGImageGetHeight(sourceImage.CGImage); 

[ yourimageview setframe:rect]; 
1

你可以继承你的形象和做eachTime以下重写新的图像设置:

@interface MyImageSubClass : UIImageView; 
@end 

@implementation MyImageSubClass 

- (void)setImage:(UIImage *)image { 
    // Let the original UIImageView parent class do its job 
    [super image]; 

    // Now set the frame according to the image size 
    // and keeping the original position of your image frame 
    if (image) { 
     CGRect r = self.frame; 
     CGSize imageSize = [image size]; 
     self.frame = (r.origin.x, r.origin.y, imageSize.width, imageSize.height); 
    } 
} 

@end 
相关问题