在屏幕尺寸上缩放 UIImage
我实现了一个小方法,可以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该在将 imageview 插入视图之前插入以下代码。
您还可以根据需要从 ScaleToFill 到 AspectFill 设置/更改。
问题就会得到解决。
you should insert following code before inserting imageview to the view.
Also you can set/change from ScaleToFill to AspectFill or as you needed.
and problem will be soved.
只需将frame更改为屏幕大小即可,即imgView.frame = self.view.frame
Just change the frame to the screen size, i.e. imgView.frame = self.view.frame
看看这是我的课:
look this is my class: