在屏幕大小上缩放UIImage

时间:2010-09-21 07:00:30

标签: iphone uiimage scaling

我已经实现了一个导入图片的小方法 iPhone屏幕,但屏幕的图片有点大。 这是方法:

- (void)ladeImage {
    id path = @"http://172.23.1.63:8080/RestfulJava/pics";
    NSURL *url = [NSURL URLWithString:path];
    NSData *data = [NSData dataWithContentsOfURL:url];  
    UIImage *img = [[UIImage alloc] initWithData:data];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    [self.view addSubview:imgView];

}

但是有没有办法在iPhone屏幕上缩放图片?

EDIT1:
好的,谢谢你的帮助,但这种方法不起作用,当我将iPhone切换到横向时,她就是你的tipp的代码:

- (void)ladeImage {
    id path = @"http://172.23.1.63:8080/RestfulJava/pics";
    NSURL *url = [NSURL URLWithString:path];
    NSData *data = [NSData dataWithContentsOfURL:url];  
    UIImage *img = [[UIImage alloc] initWithData:data];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    [imgView setContentMode:UIViewContentModeScaleToFill];
    imgView.frame = self.view.frame;
    [self.view addSubview:imgView];
}

EDIT2:
看这是我的班级:

- (void)viewDidLoad {
    [self ladeImage];

    [super viewDidLoad];
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
     if(toOrientation == UIInterfaceOrientationPortrait || toOrientation == UIInterfaceOrientationPortraitUpsideDown) 
     { 
         ImgMainPhoto.frame = CGRectMake(0, 0, 320, 480); 
         [self PortraitMode];
     } else if((toOrientation == UIInterfaceOrientationLandscapeLeft ) || (toOrientation == UIInterfaceOrientationLandscapeRight)) { 
         ImgMainPhoto.frame = CGRectMake(0, 0, 480, 320); 
         [self LendscapreMode];
     }
}

- (void)ladeImage {
    id path = @"http://172.23.1.63:8080/RestfulJava/pics";
    NSURL *url = [NSURL URLWithString:path];
    NSData *data = [NSData dataWithContentsOfURL:url];  
    UIImage *img = [[UIImage alloc] initWithData:data];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    imgView.frame = self.view.frame;
    imgView.contentMode = UIViewContentModeScaleToFill;
    [self.view addSubview:imgView];
}

3 个答案:

答案 0 :(得分:3)

在将imageview插入视图之前,应插入以下代码。

[imgView setContentMode:UIViewContentModeScaleToFill]

您也可以根据需要设置/更改ScaleToFill到AspectFill。

问题将会出现问题。

答案 1 :(得分:1)

只需将帧更改为屏幕大小,即imgView.frame = self.view.frame

答案 2 :(得分:-1)

看这是我的班级:

- (void)viewDidLoad {
    [self ladeImage];

    [super viewDidLoad];
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
     if(toOrientation == UIInterfaceOrientationPortrait || toOrientation == UIInterfaceOrientationPortraitUpsideDown) 
     { 
         ImgMainPhoto.frame = CGRectMake(0, 0, 320, 480); 
         [self PortraitMode];
     } else if((toOrientation == UIInterfaceOrientationLandscapeLeft ) || (toOrientation == UIInterfaceOrientationLandscapeRight)) { 
         ImgMainPhoto.frame = CGRectMake(0, 0, 480, 320); 
         [self LendscapreMode];
     }
}

- (void)ladeImage {
    id path = @"http://172.23.1.63:8080/RestfulJava/pics";
    NSURL *url = [NSURL URLWithString:path];
    NSData *data = [NSData dataWithContentsOfURL:url];  
    UIImage *img = [[UIImage alloc] initWithData:data];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    imgView.frame = self.view.frame;
    imgView.contentMode = UIViewContentModeScaleToFill;
    [self.view addSubview:imgView];
}