2012-05-17 2 views
0

I 사용자 인터페이스 빌더는 내보기 및 이미지보기를 레이아웃합니다. 뷰에 imageView를 놓고 콘센트로 설정합니다.이미지의 크기에 따라 imageView의 크기를 동적으로 설정할 수있는 방법

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

크기가 화면에있을 항상 내가에서 설정 한 이미지 뷰의 크기 : 문제는 상관없이 이미지의 어떤 크기 (768 * 1024,1024 * 700) 내 프로그램에서 설정 없다는 것입니다 인터페이스 빌더. 이미지의 크기에 따라 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 
관련 문제