objective-c 图像的缩略图视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1215869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:00:21  来源:igfitidea点击:

Thumbnail view of images

iphoneobjective-c

提问by Tyler

I want to show some images as a thumbnail in View controller but i dont know how to do this. Can anyone please give me some ideas or sample code if possible.

我想在视图控制器中将一些图像显示为缩略图,但我不知道如何执行此操作。如果可能的话,任何人都可以给我一些想法或示例代码。

回答by Tyler

Are you asking how to create thumbnails from larger images, or about how to build a view controller which displays them nicely?

您是在问如何从较大的图像创建缩略图,还是关于如何构建一个可以很好地显示它们的视图控制器?

Building a view controller:

构建视图控制器:

You could use TTPhotoViewControllerfrom the Three20 project(description), which acts similarly to the iPhone's built in camera roll to view images.

你可以使用TTPhotoViewController该Three20项目描述),其作用类似于iPhone的内置摄像头滚动查看图像。

You can look at the Scrollingsample codefrom apple, referred to in this question about using it for thumbnails.

您可以查看来自苹果Scrolling示例代码,在这个关于将其用于缩略图的问题中提到

If you want to build one yourself, you might consider using a GridViewfrom the moriarty library, either as a large grid in a UIScrollView, or on a more efficient row-by-row basis in a UITableView. There's a previous question on optimized image loading in a UIScrollView.

如果您想自己构建一个,您可以考虑使用GridView来自Moriarty 库的 a,或者作为 a 中的大网格UIScrollView,或者在UITableView. 之前有一个关于优化图像加载的问题UIScrollView

Creating thumbnails from larger images:

从较大的图像创建缩略图:

The code-easiest way to scale down an image is to simply display it in a UIImageViewwith a frame set to the smaller size that you want - it's scaled for you.

缩小图像的代码最简单的方法是简单地将其显示在UIImageView一个框架中,该框架设置为您想要的较小尺寸 - 它已为您缩放。

If you want to save a thumbnail, or care about memory usage, you can create a hard-scaled version. The sample code below is taken from this blog post, and can be added to UIImageas a category method:

如果您想保存缩略图,或关心内存使用情况,您可以创建一个硬缩放版本。下面的示例代码取自这篇博文,可以UIImage作为分类方法添加到:

- (UIImage*) imageScaledToSize: (CGSize) newSize {
  UIGraphicsBeginImageContext(newSize);
  [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
  UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();

  return newImage;
}

Finally, here's a previous question on masking out round corners on an image, similar to the app icons used on the home screen.

最后,这是上一个关于屏蔽图像圆角的问题,类似于主屏幕上使用的应用程序图标。

Added

添加

Using an image's built-in thumbnail:

使用图像的内置缩略图:

There's a nice function called CGImageSourceCreateThumbnailAtIndexthat understands built-in thumbnails in certain image data formats. You can see some useful sample code for this under Creating a Thumbnail Image from an Image Sourcein the Image I/O Programming Guidefrom Apple.

有一个很好的函数CGImageSourceCreateThumbnailAtIndex可以理解某些图像数据格式的内置缩略图。您可以在Apple的 Image I/O Programming GuideCreating a Thumbnail Image from an Image Source下看到一些有用的示例代码。

回答by Amagrammer

There are multiple issues with thumbnails; perhaps you could clarify which ones you are most concerned about?

缩略图存在多个问题;也许您可以澄清一下您最关心哪些问题?

  1. How to display a smaller version of an existing image

  2. How to speed up paging by caching the thumbnails (instead of just dynamically shrinking the originals)

  3. How to allow the user to page through the thumbnails

  4. How to synchronize the thumbnails with the originals, in the event that your images are editable or the user can add to them

  1. 如何显示现有图像的较小版本

  2. 如何通过缓存缩略图来加速分页(而不是仅仅动态缩小原件)

  3. 如何允许用户翻阅缩略图

  4. 如果您的图像可编辑或用户可以添加到其中,如何将缩略图与原件同步