如何在设备旋转时处理UIImage

时间:2010-08-24 08:40:33

标签: iphone

我有一个内部有UIImage的UIScrollView。下面的代码正确初始化图像(它的宽度与scrollView的宽度相同):

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

    ...

    imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"map1.jpg"]];
    imageView.userInteractionEnabled = YES;
    imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth );
    [imageScrollView addSubview:imageView];

    imageScrollView.contentSize = [imageView frame].size;

    ...

    // calculate minimum scale to perfectly fit image width, and begin at that scale
    float minimumScale = [imageScrollView frame].size.width  / [imageView frame].size.width;
    //imageScrollView.maximumZoomScale = 1.0;
    imageScrollView.minimumZoomScale = minimumScale;
    imageScrollView.zoomScale = minimumScale;
}

我想要做的是在设备旋转时将尺寸设置为完全适合(如上所述)。这是我试图做的事情:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
  float newScale = [imageScrollView frame].size.width  / [imageView frame].size.width;
  imageScrollView.zoomScale = newScale;
}

由于某些原因,这根本不起作用。我能够使其工作的唯一方法是初始化另一个UIImage并再次执行所有初始化过程,这对我来说不合适。

1 个答案:

答案 0 :(得分:0)

您可能还应该更改滚动视图的.contentSize。