开发者

Scalling UIImage on the screen size

I have implemented a small method that imports an picture on the iPhone screen, but the picture is a little bit to big for the screen. This is the Method:

- (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];

}

But is there any method to scale the picture on the iPhone screen?

Edit1:

Ok thank for your helping, but this method doesn't work, when I switch the iPhone to landscape, her is the code with your 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:

look this is my class:

- (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 *u开发者_开发百科rl = [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];
}


you should insert following code before inserting imageview to the view.

[imgView setContentMode:UIViewContentModeScaleToFill]

Also you can set/change from ScaleToFill to AspectFill or as you needed.

and problem will be soved.


Just change the frame to the screen size, i.e. imgView.frame = self.view.frame


look this is my class:

- (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];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜