如何使用 UIImageView 调整图片大小以便放大和缩小?

发布于 2024-09-11 15:58:45 字数 262 浏览 5 评论 0原文

在我看来,我有一张图片。这张图片的比例为 3:2,而且分辨率很大。

我在用图像初始化 UIImage 到原始大小然后缩小 UIImageView 以使整个图像在滚动视图中可见时遇到很多麻烦。

该程序仅停留在纵向方向。

不,请不要说只使用 UIWebView。 UIWebView 不允许我在视图加载期间设置内容大小...相反,它只是按任意比例缩小图像,而且我无法找到调整比例值的方法(我不认为这是可能的)。

感谢您的任何回复!我真的很感激他们:D

In my view I have a picture.. The picture has a 3:2 scale and is huge, resolution wise.

I'm having a lot of trouble initializing a UIImage with the image to the original size and then zooming out of the UIImageView so that the entire image is visible within the scrollview.

The program stays only in the portrait orientation.

And no, please don't say to just use UIWebView. UIWebView doesn't let me set the content size during view load...instead, it just zooms out of the image by some arbitrary scale and I couldn't figure out a way to adjust the scale value (I don't think it's possible).

Thanks for any responses! I really appreciate them :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猥琐帝 2024-09-18 15:58:45

以下是放置响应捏合缩放的图像的示例。基本上,您将 UIImageView 放置在 UIScrollView 中并更改一些设置。

UIImageView *myImage;
UIScrollView *myScroll;

-(void)viewDidLoad{

  myImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,360,450)];
  [myImage setImage:[UIImage imageNamed:@"coolpic.png"]];
  myImage.contentMode = UIViewContentModeScaleAspectFit;
  [myScroll addSubview:myImage];
  [myScroll setContentSize:CGSizeMake(myImage.frame.size.width, myImage.frame.size.height)];
  [myScroll setMinimumZoomScale:1.0];
  [myScroll setMaximumZoomScale:4.0];
}

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

当然,正确设置所有委托和 IB 挂钩。

编辑:我刚刚重读了你的问题。该示例中回答您问题的部分是 UIImageView 的框架规范及其 contentMode 设置。

Here's an example of placing an image that responds to pinch-to-zoom. Basically, you place the UIImageView in a UIScrollView and change some settings.

UIImageView *myImage;
UIScrollView *myScroll;

-(void)viewDidLoad{

  myImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,360,450)];
  [myImage setImage:[UIImage imageNamed:@"coolpic.png"]];
  myImage.contentMode = UIViewContentModeScaleAspectFit;
  [myScroll addSubview:myImage];
  [myScroll setContentSize:CGSizeMake(myImage.frame.size.width, myImage.frame.size.height)];
  [myScroll setMinimumZoomScale:1.0];
  [myScroll setMaximumZoomScale:4.0];
}

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

Of course, set all your delegates and IB hooks properly.

Edit: I just reread your question. The portion of the example that answers your question is the frame specification of the UIImageView and its contentMode setting.

晒暮凉 2024-09-18 15:58:45

您几乎肯定不希望在加载时一次性加载“巨大的、分辨率明智的”图像,无论其大小如何。我建议查看一些有关此内容的 Apple 示例代码(从 ScrollViewSuite 会很好,我想说)。

WWDC 最近发布了一段视频,他们在其中实时实现了此类功能(他们有更多的照片查看应用程序,但您也可以很容易地只用一张图像来完成他们所展示的内容),所以也请看一下。

You almost certainly don't want to load your 'huge, resolution wise' image all at once on load regardless of it's scale. I'd suggest checking out some of Apple's sample code on this stuff (starting with ScrollViewSuite would be good, I'd say).

There was also a recent video released from WWDC where they implement this sort of thing live (they have more of a photo viewing app, but you could pretty easily do what they show with just one image too) so take a look for that too.

等数载,海棠开 2024-09-18 15:58:45

转到 UIImageView 的属性选项卡。选择“填充方面”或“适合方面”。如果您使用“宽高比填充”,则可能无法显示整个图像。我只是使用“适合的方面”,并将背景设为黑色。

Go to the attributes tab of the UIImageView. Select "aspect to fill" or "aspect to fit". If you use "aspect to fill", you might not show the whole image. I just use "aspect to fit", and make the background black.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文