当设备方向改变时,图像被拉伸,模式设置为重绘

时间:2012-07-10 19:11:41

标签: ios uiscrollview uiimageview uiimageorientation

我在UIImageView中嵌入了UIScrollView ...当加载image时,zoom scale被设置为image的最大数量}是可见的,但是当旋转设备时,不保持方面并最终显示拉伸。视图模式设置为redraw

- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.delegate = self;
// get the url of the photo from flickr
NSURL* url = [FlickrFetcher urlForPhoto:self.photo format:FlickrPhotoFormatLarge];
// set the scrollview's title to be the title of the picture
self.title = [self.photo objectForKey:FLICKR_PHOTO_TITLE];
// convert the url into an image
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
// set the image as the image in the imageView
[self.imageView setImage:image];
// we will be scrolling over the size of the image
self.scrollView.contentSize = self.imageView.image.size;
}

-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
UIInterfaceOrientation currOrient = self.interfaceOrientation;
self.imageView.frame = CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
if (currOrient == UIInterfaceOrientationPortrait || currOrient == UIInterfaceOrientationPortraitUpsideDown)
    [self.scrollView setZoomScale: self.scrollView.frame.size.height / self.imageView.frame.size.height];
else {
    [self.scrollView setZoomScale: self.scrollView.frame.size.width / self.imageView.frame.size.width];

}
//     [self.imageView setNeedsDisplay];
// [self.scrollView setNeedsDisplay];

}

-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
scrollView.transform = CGAffineTransformIdentity;
}

1 个答案:

答案 0 :(得分:0)

在您的控制器中,您必须实施:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration

将缩放比例设置为viewWillAppear的动画。