ios - 在 ScrollView iOS sdk 中缩放图像

标签 ios iphone uiimageview scrollview

这个问题在这里已经有了答案:





UIScrollView setZoomScale:animated messes scrollView aspect ratio

(1 个回答)


8年前关闭。




以适当的比例缩放 ScrollView iOS sdk 中的图像。在 ScrollView 中放大图像时如何保持图像的比例。

最佳答案

Declare following variable in .h file.

UIScrollView *scrollViewPhoto;
UIImageView *imvPhoto;

put the following code in .m file and call set image method when setting the image in imageView.


#pragma mark - set Image view
-(void)setImage:(UIImage *)imageV
{
    scrollViewPhoto.zoomScale = 1;
    scrollViewPhoto.hidden = NO;
    toolbar.hidden = NO;
    UIImage* image = imageV;
    imvPhoto.image = imageV;

    float width =  scrollViewPhoto.frame.size.width/image.size.width;
    if (image.size.width<self.view.frame.size.width) {
        width = 1;
    }

    float height = scrollViewPhoto.frame.size.height/image.size.height;
    if (image.size.height<self.view.frame.size.height) {
        height = 1;
    }

    float f = 0;

    if (width < height)
    {
        f = width;
    }
    else
    {
        f = height;
    }

    imvPhoto.frame = CGRectMake(0, 0, image.size.width*f, image.size.height*f);


    CGRect frame = scrollViewPhoto.frame;
    scrollViewPhoto.contentSize = frame.size;
    imvPhoto.center = CGPointMake(frame.size.width/2, frame.size.height/2);
}

-(void)setImageViewCenter{
    CGRect frame = scrollViewPhoto.frame;
    imvPhoto.center = CGPointMake(imvPhoto.center.x , frame.size.height/2);
}


- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{

    float width = 0;
    float height = 0;

    if (imvPhoto.frame.size.width < scrollView.frame.size.width)
    {
        width = scrollView.frame.size.width;

        if (imvPhoto.frame.size.height < scrollView.frame.size.height)
        {
            height = scrollView.frame.size.height;
        }
        else
        {
            height = scrollView.contentSize.height;
        }
    }
    else
    {
        width = scrollView.contentSize.width;

        if (imvPhoto.frame.size.height < scrollView.frame.size.height)
        {
            height = scrollView.frame.size.height;
        }
        else
        {
            height = scrollView.contentSize.height;
        }

    }

    imvPhoto.center = CGPointMake(width/2, height/2);

}


-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView {
return imvPhoto;
}

关于ios - 在 ScrollView iOS sdk 中缩放图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19631033/

相关文章:

ios - 在 iOS 上制作通用应用程序

ios - 构建后 XCode iOS 颜色不匹配

ios - UIImageView 上的点击手势

ios - 圆形 UIView 上的 CGAffineTransformMakeScale 动画为正方形

ios - 解析函数 `findObjectsInBackgroundWithBlock:` 检索部分数据库对象,但不是全部

ios - 如何获取用户 didSelectRowAtIndexPath 的次数?

ios - MBProgressHUD 更新进度

iphone - 如何在 iphone 中将数据传递给 Tbxml

javascript - 如何检测 iPhone 上页面滚动的比例?

ios - 如何检查当前单击了哪个 uiimageview?