图像的缩略图视图 [英] Thumbnail view of images

查看:88
本文介绍了图像的缩略图视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在View控制器中将一些图像显示为缩略图,但我不知道该怎么做。如果可能,任何人都可以给我一些想法或示例代码。

解决方案

您是否在询问如何从较大的图像创建缩略图,或者关于如何构建一个能够很好地显示它们的视图控制器?



构建视图控制器:



您可以使用中的 TTPhotoViewController Three20项目 description ),其行为类似于iPhone的内置相机胶卷以查看图像。



您可以查看 滚动示例代码,参见这个问题关于将它用于缩略图



如果你想自己构建一个,你可以考虑使用 GridView 来自 moriarty库,或者作为 UIScrollView 中的大网格,或者以更高效的逐行为基础在 UITableView 中。有一个关于 UIScrollView



从较大的图片创建缩略图



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



如果要保存缩略图或关心内存使用情况,可以创建硬盘版本。以下示例代码取自此博客文章,并且可以作为类别方法添加到 UIImage

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

返回newImage;
}

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



已添加



使用图片的内置缩略图:



有一个很好的函数叫做 CGImageSourceCreateThumbnailAtIndex 了解某些图像数据格式的内置缩略图。您可以在从图像源创建缩略图图像下看到一些有用的示例代码。来自Apple的Conceptual / ImageIOGuide / imageio_source / ikpg_source.html#// apple_ref / doc / uid / TP40005462-CH218-DontLinkElementID_5rel =nofollow noreferrer>图像I / O编程指南


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.

解决方案

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 TTPhotoViewController from the Three20 project (description), which acts similarly to the iPhone's built in camera roll to view images.

You can look at the Scrolling sample code from apple, referred to in this question about using it for thumbnails.

If you want to build one yourself, you might consider using a GridView from 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.

Creating thumbnails from larger images:

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

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 UIImage as a category method:

- (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 CGImageSourceCreateThumbnailAtIndex that 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 Source in the Image I/O Programming Guide from Apple.

这篇关于图像的缩略图视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆