gpt4 book ai didi

ios - 在 ScrollView iOS sdk 中缩放图像

转载 作者:行者123 更新时间:2023-12-01 19:05:57 27 4
gpt4 key购买 nike

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





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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com