在屏幕尺寸上缩放 UIImage

发布于 2024-09-24 06:19:57 字数 2141 浏览 2 评论 0原文

我实现了一个小方法,可以在 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屏幕上缩放图片呢?

编辑1:
好的,谢谢您的帮助,但是这个方法不起作用,当我将 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];
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

⊕婉儿 2024-10-01 06:19:57

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

[imgView setContentMode:UIViewContentModeScaleToFill]

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

问题就会得到解决。

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.

漫雪独思 2024-10-01 06:19:57

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

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

小伙你站住 2024-10-01 06:19:57

看看这是我的课:

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

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文