在 UIScrollView 中缩放和滚动大图像

发布于 2024-12-03 08:54:16 字数 506 浏览 0 评论 0原文

我已经被困在这个问题上有一段时间了。我查看了许多其他答案以及苹果的示例。

我想知道的是如何在 UIScrollView 中加载大图像,并能够翻阅它们并放大它们,就像照片应用程序一样。

我正在开发一个应用程序,我需要滚动浏览大图像(大图像,我的意思是大于 1024 x 1024 的图像)。我已经实现了类似于照片应用程序的功能。然而,现在我使用大图像,我收到内存警告和其他东西。我知道为什么收到警告,并且我知道我需要平铺图像

Apple 的示例演示了如何使用已经存在的小图像进行平铺。我的应用程序将下载多个图像,将它们保存在磁盘上,用户稍后可以查看它们。因此,我无法使用其他程序剪切图像。

我需要一些可以平铺全尺寸图像的东西。任何帮助将不胜感激。此外,正如我之前提到的,我知道我必须对平铺做一些事情。但是,我对此很陌生,如果答案包含一些示例代码,我将非常感激,我可以将其用作起点。

我查看了其他问题,似乎没有一个问题的答案令我满意,因此我再次问这个问题。

再次感谢!

I've been stuck at this for quite some time. I've looked at many other answers and also at Apple's examples.

What I want to know is how I can go about loading large images in UIScrollView, and be able to page through them and zoom in to them, like the Photos app.

I'm working on an app where I need to scroll through large images (by large, I mean images greater than 1024 x 1024). I've already implemented something similar to what the photos app does. However, now that I'm using large images, I get memory warnings and stuff. I know why I'm getting the warning, and I know that I need to Tile the images.

Apple's examples demonstrate tiling with small images which are already present. My app will be downloading multiple images, saving them on a disk and the user will be able to look at them at a later time. Thus, I cannot have the images cut up using other programs.

I need something wherein I can tile the full size images. Any help will be greatly appreciated. Additionally, as I mentioned earlier, I know that I have to do something with Tiling. However, I'm new to this, and I would greatly appreciate if the answers contain some sample code, which I can use as a start off point.

I've looked at other questions, and none seemed to have been answered to my satisfaction, owing to which, I'm asking this question again.

Thanks again!

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

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

发布评论

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

评论(1

极度宠爱 2024-12-10 08:54:16

要使用平铺,您需要在视图中使用CaTiledLayer。谷歌搜索会给你提供很好的信息。基本上,您声明 UIVIew 正在使用它,声明一些用于缩放的细节级别,并且在 drawRect 中您将收到每个图块作为 rect 参数。

要平铺图像,请加载 UIImage(它使用内存,但比显示它要少得多)并用于您想要的每个平铺,例如:

UIGraphicsBeginImageContextWithOptions(tileSize, YES, 1);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, -tileSize.width * columnNumber, -tileSize.height*rowNumber);
[img drawInRect:CGRectMake(0, 0, tileSize.width, tileSize.height)];
UIImage* imgCut = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

并保存 imgCut。如果您在 CATiledLayer 中使用不同的细节级别,您可能会希望生成具有不同缩放比例的平铺。

To work with tiling you'll need to use a CaTiledLayer in your view. Googling for it will give you good information. Basically you declare that the UIVIew is using it, declare some levels of details for zooming and in the drawRect you'll receive each tile as the rect parameter.

To tile the image, load in a UIImage (it uses memory, but quite less than showing it) and use for each tile you want something like:

UIGraphicsBeginImageContextWithOptions(tileSize, YES, 1);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, -tileSize.width * columnNumber, -tileSize.height*rowNumber);
[img drawInRect:CGRectMake(0, 0, tileSize.width, tileSize.height)];
UIImage* imgCut = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

and save imgCut. You probably will want to generate tiling with different zooms if you use different level of detail in your CATiledLayer.

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