UIImage 在开始时不适合 UIScrollView
我有一个像这样的图像:
当我将图像加载到 uiimageview 然后作为子视图添加到 uiscrollview 时,开始图像显示如下:
问题是我想在开始时看到所有适合屏幕的图像但它显示已经缩放。有没有办法处理这个问题请帮忙...
我有这样的代码:
[self.view addSubview:scrollView];
UIImageView *tempImageView =
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Tree.jpg"]];
self.imageView = tempImageView;
[tempImageView release];
[scrollView setContentMode:UIViewContentModeScaleAspectFit];
[imageView sizeToFit];
scrollView.backgroundColor=[UIColor blackColor];
scrollView.contentSize = imageView.frame.size;
scrollView.minimumZoomScale = scrollView.frame.size.width / imageView.frame.size.width;
scrollView.maximumZoomScale = 4.0;
[scrollView setZoomScale:1.0];
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:imageView];
I have an image like:
When I load the image to uiimageview and then adding as a subview to uiscrollview, at start the image is showing like:
The problem is I want to see all the image fit to screen at start but it is showing already zoomed. Is there a way to handle this please help ...
I have the code like:
[self.view addSubview:scrollView];
UIImageView *tempImageView =
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Tree.jpg"]];
self.imageView = tempImageView;
[tempImageView release];
[scrollView setContentMode:UIViewContentModeScaleAspectFit];
[imageView sizeToFit];
scrollView.backgroundColor=[UIColor blackColor];
scrollView.contentSize = imageView.frame.size;
scrollView.minimumZoomScale = scrollView.frame.size.width / imageView.frame.size.width;
scrollView.maximumZoomScale = 4.0;
[scrollView setZoomScale:1.0];
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:imageView];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
也许您正在寻找这个,
Probably you are looking for this,
看一下UIView的contentMode-Property(UIImageView也支持这个)。
这应该有效...
Have a look at the contentMode-Property of UIView (UIImageView also supports this).
This should work...
我认为问题出在
Apple 的文档中说“此方法调整接收器的框架以匹配指定图像的大小。默认情况下它还会禁用图像视图的用户交互。”
改为使用
I think the problem is in
Apple's docs say "This method adjusts the frame of the receiver to match the size of the specified image. It also disables user interactions for the image view by default."
instead use
请注意,还有 [scrollView ZoomToRect:animated:] 描述 此处
您可以将其设置为图像的外边界,以便它会自动适应您的视图中的图像。但请确保按照上面的 Deepak 解决方案正确设置 MaximumZoomScale。
Note that there is also [scrollView zoomToRect:animated:] described here
You can set it to the outer bounds of your image so that it will auto-fit your image in your view. But make sure to setup correctly the maximumZoomScale as per Deepak solution above.
我使用 Deepak 的解决方案和 Apple 的建议,并将 UIScrollView 子类化,其中包含 UIImageView 。但是,我仅在滚动视图子类的第一个布局上缩放图像,并且仅当帧大小小于图像大小时:
这就是我初始化子类的方式:
I used Deepak's solution and Apples recommendations and subclassed UIScrollView with UIImageView inside it. However, I zoom the image only on first layout of my scrollview subclass and only if frame size is smaller than image size:
This is how I init my subclass: