是否可以在 UIScrollView 中平铺图像而无需手动创建

时间:2022-12-30
本文介绍了是否可以在 UIScrollView 中平铺图像而无需手动创建所有平铺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 iPhone 示例代码PhotoScroller" 来自 WWDC 2010,他们展示了如何通过滚动、缩放和分页来很好地模仿照片应用程序.他们还将图像平铺以展示如何显示高分辨率图像并保持良好的性能.

In the iPhone sample code "PhotoScroller" from WWDC 2010, they show how to do a pretty good mimmic of the Photos app with scrolling, zooming, and paging of images. They also tile the images to show how to display high resolution images and maintain good performance.

在示例代码中通过抓取不同分辨率的预缩放和剪切图像并将它们放置在构成整个图像的网格中来实现平铺.

Tiling is implemented in the sample code by grabbing pre scaled and cut images for different resolutions and placing them in the grid which makes up the entire image.

我的问题是:有没有一种方法可以平铺图像而无需手动浏览所有照片并创建平铺"?照片应用程序如何能够即时显示大图像?

My question is: is there a way to tile images without having to manually go through all your photos and create "tiles"? How is it the Photos app is able to display large images on the fly?

编辑以下是 Deepa 回答的代码:

Edit Here is the code from Deepa's answer below:

- (UIImage *)tileForScale:(float)scale row:(int)row col:(int)col size:(CGSize)tileSize  image:(UIImage *)inImage
{
CGRect subRect = CGRectMake(col*tileSize.width, row * tileSize.height, tileSize.width, tileSize.height);
CGImageRef tiledImage = CGImageCreateWithImageInRect([inImage CGImage], subRect);
UIImage *tileImage = [UIImage imageWithCGImage:tiledImage]; 
return tileImage;
}

推荐答案

下面是生成平铺图像的代码:

Here goes the piece of code for tiled image generation:

在 PhotoScroller 源代码中,将 tileForScale: row:col: 替换为以下内容:

In PhotoScroller source code replace tileForScale: row:col: with the following:

inImage - 您要创建图块的图像

inImage - Image that you want to create tiles

- (UIImage *)tileForScale: (float)scale row: (int)row column: (int)col size: (CGSize)tileSize image: (UIImage*)inImage
{
    CGRect subRect = CGRectMake(col*tileSize.width, row * tileSize.height, tileSize.width, tileSize.height);
    CGImageRef tiledImage = CGImageCreateWithImageInRect([inImage CGImage], subRect);
    UIImage *tileImage = [UIImage imageWithCGImage: tiledImage];
    return tileImage;
}

问候,迪帕

这篇关于是否可以在 UIScrollView 中平铺图像而无需手动创建所有平铺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:如何像 uitableviewcell 一样重用/回收自定义元素? 下一篇:UIScrollView 的子项的尾随空间限制错误

相关文章

最新文章