繁体   English   中英

在屏幕尺寸上调用UIImage

[英]Scalling UIImage on the screen size

我实现了一种在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切换到横向时,她就是您的提示代码:

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

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

[imgView setContentMode:UIViewContentModeScaleToFill]

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

问题将会解决。

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

看这是我的课:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM