开发者

How To Handle UIImage on device rotation

I have a UIScrollView with a UIImage inside. The following code initializes the image properly (it's width is the same as the width of the 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 scal开发者_如何学运维e 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;
}

What I want to do is to set the size to perfectly fit (like above) when the device rotates. Here's what I've tried to do:

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

For some reasons this doesn't work at all. The only way that I was able to make it work was to initialize another UIImage and do all the initialization process again, which doesn't feel right to me.


Probably you should also change .contentSize of scroll view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜