image is getting stretched when device orientation is changed, mode is set to redraw
-
20-06-2021 - |
سؤال
I have a UIImageView
embedded in a UIScrollView
... When the image
is loaded the zoom scale
is set so that the maximum amount of the image
is visible, however when the device is rotated the aspect is not maintained and ends up appearing stretched. Mode for views is set to 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;
}
المحلول
In your controller you will have to implement:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
Set your zoom scale there as an animation of viewWillAppear.
لا تنتمي إلى StackOverflow