UIImageView pinch zooming in UIScrollView

纵然是瞬间 提交于 2019-12-10 10:57:24

问题


I am comfortable with pinch zooming functionality using UIScrollView. But the problem is the aspect fit of image in scrollview.

Currently, I am having this, below image:

But I want image to be fit in screen like below image:

And same behavior for landscape. How can I achieve this?

Below is the code:

- (void)viewDidLoad
{
    UIImage *image = [UIImage imageWithData:appDelegate.selectedOriginalImage];
    imgView.image = image;
    CGSize imageSize = image.size;
    CGRect rect = CGRectMake(0, 0, imageSize.width, imageSize.height);
    imgView.frame = rect;
    scrollView.contentSize = CGSizeMake(imgView.frame.size.width, imgView.frame.size.height);
    scrollView.maximumZoomScale = 4.0;
    scrollView.minimumZoomScale = 1.0;
    scrollView.delegate = self;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    UIImage *image = [UIImage imageWithData:appDelegate.selectedOriginalImage];
    CGSize imageSize = image.size;
    CGRect rect = CGRectMake(0, 0, imageSize.width, imageSize.height);
    imgView.frame = rect;

    [scrollView setContentOffset:CGPointMake(0, 0)]; 

    return YES;
}

回答1:


You are setting the UIImageView to the original size of the image instead of the size of the UIScrollView that contains it.

- (void)viewDidLoad
{
    UIImage *image = [UIImage imageWithData:appDelegate.selectedOriginalImage];
    imgView.image = image;

    imgView.frame = scrollView.bounds;
    [imgView setContentMode:UIViewContentModeScaleAspectFit];
    scrollView.contentSize = CGSizeMake(imgView.frame.size.width, imgView.frame.size.height);
    scrollView.maximumZoomScale = 4.0;
    scrollView.minimumZoomScale = 1.0;
    scrollView.delegate = self;
}

remember to return the imgView on the viewToZoom delegate method



来源:https://stackoverflow.com/questions/13746085/uiimageview-pinch-zooming-in-uiscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!