在整个屏幕上以横向模式显示图片

时间:2010-09-21 09:37:03

标签: iphone uiimageview autorotate

我实现了一个小代码来显示图片,但是为什么肖像模式下的图片针对iPhone屏幕进行了优化而未在横向模式下进行优化可以帮助我,这里是代码:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
        imageView.frame = CGRectMake(0, 0, 320, 480); 
        imageView.contentMode = UIViewContentModeScaleToFill;
        return YES;
    } else if((interfaceOrientation == UIInterfaceOrientationLandscapeLeft ) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight)) { 
        imageView.contentMode = UIViewContentModeScaleToFill;
        imageView.frame = CGRectMake(0, 0, 480, 320); 
        return YES;
    }
    return YES;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [self ladeImage];

    [super viewDidLoad];
}

- (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];
    imageView = [[UIImageView alloc] initWithImage:img];
    imageView.frame = self.view.frame;
    [self.view addSubview:imageView];
}

1 个答案:

答案 0 :(得分:1)

当iPhone旋转时,通常仿射变换矩阵包含旋转所需的变换。这意味着,使用您的代码,视图的变换将以旋转的方式更改,但由于您也旋转图像,因此将其旋转回来。

试试这个:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  return YES;
}