如何在 viewController 中显示与下面的屏幕截图相同的图像?

发布于 2024-11-11 07:13:12 字数 328 浏览 6 评论 0原文

在我的 iPad 程序中,我有一个保存图像的数组。如何在 viewController 中显示所有图像(来自图像数组),与下面的屏幕截图相同?有没有好的方法可以参考,有任何示例应用程序路径或示例代码吗?

我需要以下功能。

  1. 点击图像将全屏显示它
  2. 关闭按钮可关闭此视图,与屏幕截图相同。
  3. 两个按钮用于显示剩余图像和先前图像。

下面附有示例屏幕截图。我们可以看到下面的应用程序在屏幕右侧显示所有图像。

在此处输入图像描述

In my iPad program I have an array which holds images. How can I display my all images (from an image array) in my viewController the same as the below screen shot? Is there a good way to do it, any sample application paths to refer or sample code?

I need the following functionality.

  1. Tapping an image will show it full screen
  2. A close button to close this view as the same as the screen shot.
  3. Two buttons to display the remaining and previous images.

A sample screen shot is attached below. We can see that the below application is showing all images at the right side of the screen.

enter image description here

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

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

发布评论

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

评论(1

打小就很酷 2024-11-18 07:13:12

好吧,这应该很容易,尽管需要一些编码。
从简单的基于视图应用模板开始。

我不知道图像来自哪里,所以我只是将所有要显示的图像放在资源文件夹中。

视图控制器 PictureViewController 将接受 UIImage 数组,并应以模态方式呈现。我将在下面发布代码。

显示 PictureViewController 的代码放置在模板创建的视图控制器中。我刚刚向视图添加了一个简单的 UIButton 以触发如下所示的操作:

- (IBAction)onShowPictures
{
    // Load all images from the bundle, I added 14 images, named 01.jpg ... 14.jpg
    NSMutableArray *images = [NSMutableArray array];
    for (int i = 1; i <= 14; i++) {
        [images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%02d.jpg", i]]];
    }

    PictureViewController *picViewController = [[PictureViewController alloc] initWithImages:images];

    [self presentModalViewController:picViewController animated:YES];

    [picViewController release];
}

视图控制器的视图是使用界面生成器创建的,如下所示:

PictureViewController.xib

在此处输入图像描述

查看层次结构

在此处输入图像描述

全屏图像链接到以下头文件中看到的插座fullScreenImage。动作也相互关联。

我已将全屏 imageView 的内容模式设置为 Aspect Fit。

缩略图将通过代码动态添加。下面是视图控制器的代码

PictureViewController.h

@interface PictureViewController : UIViewController 
{
    NSMutableArray *imageViews;

    NSArray *images;

    UIImageView *fullScreenImage;

    int currentPage;
}

@property (nonatomic, retain) NSMutableArray *imageViews;
@property (nonatomic, retain) NSArray *images;
@property (nonatomic, retain) IBOutlet UIImageView *fullScreenImage;

- (id)initWithImages:(NSArray *)images;

- (IBAction)onClose;
- (IBAction)onNextThumbnails;
- (IBAction)onPreviousThumbnails;

@end

PictureViewController.m

顶部的定义 MAX_THUMBNAILS 定义了所看到的缩略图的最大数量。 UITapGestureRecognizer 将处理缩略图的点击事件。调整 setupThumbnailImageViews 中的 CGRectMake 以根据需要放置缩略图。该控制器只是一种基本方法,没有方向支持。

#import "PictureViewController.h"

#define MAX_THUMBNAILS 6

@interface PictureViewController ()

- (void)showSelectedImageFullscreen:(UITapGestureRecognizer *)gestureRecognizer;
- (void)setupThumbnailImageViews;
- (void)setThumbnailsForPage:(int)aPage;
- (UIImage *)imageForIndex:(int)anIndex;

@end


@implementation PictureViewController

@synthesize imageViews, images, fullScreenImage;


- (id)initWithImages:(NSArray *)someImages
{
    self = [super init];
    if (self) {
        self.images = someImages;
        self.imageViews = [NSMutableArray array];
        currentPage = 0;
    }
    return self;
}

- (void)viewDidLoad
{
    self.fullScreenImage.image = [images objectAtIndex:0];
    [self setupThumbnailImageViews];
    [self setThumbnailsForPage:0];
}

- (void)showSelectedImageFullscreen:(UITapGestureRecognizer *)gestureRecognizer
{
    UIImageView *tappedImageView = (UIImageView *)[gestureRecognizer view];

    fullScreenImage.image = tappedImageView.image;
}

- (void)setupThumbnailImageViews
{
    for (int i = 0; i < MAX_THUMBNAILS; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20.0, 
                                                                               166.0 + (130.0 * i), 
                                                                               130.0, 
                                                                               90.0)];

        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self 
                                                                                               action:@selector(showSelectedImageFullscreen:)];
        tapGestureRecognizer.numberOfTapsRequired = 1;
        tapGestureRecognizer.numberOfTouchesRequired = 1;
        [imageView addGestureRecognizer:tapGestureRecognizer];
        [tapGestureRecognizer release];

        imageView.userInteractionEnabled = YES;

        [imageViews addObject:imageView];
        [self.view addSubview:imageView];
    }
}

- (void)setThumbnailsForPage:(int)aPage
{
    for (int i = 0; i < MAX_THUMBNAILS; i++) {
        UIImageView *imageView = (UIImageView *)[imageViews objectAtIndex:i];
        UIImage *image = [self imageForIndex:aPage * MAX_THUMBNAILS + i];

        if (image) {
            imageView.image = image;
            imageView.hidden = NO;          
        } else {
            imageView.hidden = YES;
        }
    }
}

- (UIImage *)imageForIndex:(int)anIndex
{
    if (anIndex < [images count]) {
        return [images objectAtIndex:anIndex];
    } else {
        return nil;
    }
}


#pragma mark -
#pragma mark user interface interaction

- (IBAction)onClose
{
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)onNextThumbnails
{
    if (currentPage + 1 <= [images count] / MAX_THUMBNAILS) {
        currentPage++;
        [self setThumbnailsForPage:currentPage];
    }
}

- (IBAction)onPreviousThumbnails
{
    if (currentPage - 1 >= 0) {
        currentPage--;  
        [self setThumbnailsForPage:currentPage];    
    }
}


#pragma mark -
#pragma mark memory management

- (void)didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning];

    [images release];   
}

- (void)viewDidUnload 
{
    [super viewDidUnload];

    [imageViews release];   
    [fullScreenImage release];
}

- (void)dealloc 
{
    [images release];
    [imageViews release];   
    [fullScreenImage release];

    [super dealloc];
}

@end

结果:

在此处输入图像描述

Alright, this should be easy, though a bit of coding is needed.
Start out with a simple view based app template.

I don't know where the images come from so I just put all the images to be shown in the resources folder.

The view controller PictureViewController will accept an array of UIImages and should be presented modally. I'll post the code below.

The code to show the PictureViewController is placed in the view controller created by the template. I just added a simple UIButton to the view to trigger an action which looks something like this:

- (IBAction)onShowPictures
{
    // Load all images from the bundle, I added 14 images, named 01.jpg ... 14.jpg
    NSMutableArray *images = [NSMutableArray array];
    for (int i = 1; i <= 14; i++) {
        [images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%02d.jpg", i]]];
    }

    PictureViewController *picViewController = [[PictureViewController alloc] initWithImages:images];

    [self presentModalViewController:picViewController animated:YES];

    [picViewController release];
}

The view controller's view was created with interface builder, looking like this:

PictureViewController.xib

enter image description here

View Hierarchy

enter image description here

The fullscreen image is linked to an outlet fullScreenImage seen in the following header file. Actions are connected as well.

I've set the fullscreen imageView's content mode to Aspect Fit.

The Thumbnails will be added dynamically with code. Here's the view controller's code

PictureViewController.h

@interface PictureViewController : UIViewController 
{
    NSMutableArray *imageViews;

    NSArray *images;

    UIImageView *fullScreenImage;

    int currentPage;
}

@property (nonatomic, retain) NSMutableArray *imageViews;
@property (nonatomic, retain) NSArray *images;
@property (nonatomic, retain) IBOutlet UIImageView *fullScreenImage;

- (id)initWithImages:(NSArray *)images;

- (IBAction)onClose;
- (IBAction)onNextThumbnails;
- (IBAction)onPreviousThumbnails;

@end

PictureViewController.m

The define MAX_THUMBNAILS on top defines the maximum of thumbnails seen. A UITapGestureRecognizer will take care of the tap events for the thumbnails. Adjust the CGRectMake in setupThumbnailImageViews to position the thumbnails as you wish. This controller is just a basic approach without orientation support.

#import "PictureViewController.h"

#define MAX_THUMBNAILS 6

@interface PictureViewController ()

- (void)showSelectedImageFullscreen:(UITapGestureRecognizer *)gestureRecognizer;
- (void)setupThumbnailImageViews;
- (void)setThumbnailsForPage:(int)aPage;
- (UIImage *)imageForIndex:(int)anIndex;

@end


@implementation PictureViewController

@synthesize imageViews, images, fullScreenImage;


- (id)initWithImages:(NSArray *)someImages
{
    self = [super init];
    if (self) {
        self.images = someImages;
        self.imageViews = [NSMutableArray array];
        currentPage = 0;
    }
    return self;
}

- (void)viewDidLoad
{
    self.fullScreenImage.image = [images objectAtIndex:0];
    [self setupThumbnailImageViews];
    [self setThumbnailsForPage:0];
}

- (void)showSelectedImageFullscreen:(UITapGestureRecognizer *)gestureRecognizer
{
    UIImageView *tappedImageView = (UIImageView *)[gestureRecognizer view];

    fullScreenImage.image = tappedImageView.image;
}

- (void)setupThumbnailImageViews
{
    for (int i = 0; i < MAX_THUMBNAILS; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20.0, 
                                                                               166.0 + (130.0 * i), 
                                                                               130.0, 
                                                                               90.0)];

        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self 
                                                                                               action:@selector(showSelectedImageFullscreen:)];
        tapGestureRecognizer.numberOfTapsRequired = 1;
        tapGestureRecognizer.numberOfTouchesRequired = 1;
        [imageView addGestureRecognizer:tapGestureRecognizer];
        [tapGestureRecognizer release];

        imageView.userInteractionEnabled = YES;

        [imageViews addObject:imageView];
        [self.view addSubview:imageView];
    }
}

- (void)setThumbnailsForPage:(int)aPage
{
    for (int i = 0; i < MAX_THUMBNAILS; i++) {
        UIImageView *imageView = (UIImageView *)[imageViews objectAtIndex:i];
        UIImage *image = [self imageForIndex:aPage * MAX_THUMBNAILS + i];

        if (image) {
            imageView.image = image;
            imageView.hidden = NO;          
        } else {
            imageView.hidden = YES;
        }
    }
}

- (UIImage *)imageForIndex:(int)anIndex
{
    if (anIndex < [images count]) {
        return [images objectAtIndex:anIndex];
    } else {
        return nil;
    }
}


#pragma mark -
#pragma mark user interface interaction

- (IBAction)onClose
{
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)onNextThumbnails
{
    if (currentPage + 1 <= [images count] / MAX_THUMBNAILS) {
        currentPage++;
        [self setThumbnailsForPage:currentPage];
    }
}

- (IBAction)onPreviousThumbnails
{
    if (currentPage - 1 >= 0) {
        currentPage--;  
        [self setThumbnailsForPage:currentPage];    
    }
}


#pragma mark -
#pragma mark memory management

- (void)didReceiveMemoryWarning 
{
    [super didReceiveMemoryWarning];

    [images release];   
}

- (void)viewDidUnload 
{
    [super viewDidUnload];

    [imageViews release];   
    [fullScreenImage release];
}

- (void)dealloc 
{
    [images release];
    [imageViews release];   
    [fullScreenImage release];

    [super dealloc];
}

@end

The result:

enter image description here

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