macOS学习笔记(9)简易图片查看器

省略建工程,添加图片以及ui设计部分。

#import <Cocoa/Cocoa.h>

@interface ViewController : NSViewController
{
    IBOutlet NSImageView *imageview;
    NSArray * imagePaths;//图像所在路径的引用
    NSMutableArray *images;//图像的引用
    int count;
    IBOutlet NSTextField *num;
}
-(IBAction)nextImage:(id)sender;
-(IBAction)beforeImage:(id)sender;
-(IBAction)numImage:(id)sender;
@end

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
}


- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}

-(void)awakeFromNib
{
    NSBundle * mainBundle = [NSBundle mainBundle];
    //引用图像的路径
//    NSString * path = [mainBundle pathForResource:@"Abstract 1" ofType:@"png"];//查看文件名为Abstract.png的图片
//    NSImage * image = [[NSImage alloc] initWithContentsOfFile:path];
    //获得文件包的引用,在文件包中默认文件夹resource查询png后缀的文件,返回一个数组
    imagePaths = [mainBundle pathsForResourcesOfType:@"png" inDirectory:nil];
    //保留引用
    [imagePaths retain];
    count = 0;
    //用数组第一个元素对应的文件内容初始化一个nsimage类,得到一个实例
    NSImage * image =[[NSImage alloc] initWithContentsOfFile:[imagePaths objectAtIndex:count]];
    //显示图片,然后销毁这个实例
    [imageview setImage:image];
    [image release];
}

/*
//用图像的引用来显示图片,提高性能
-(void)awakeFromNib
{
    NSBundle * mainBundle = [NSBundle mainBundle];
    NSArray * imagePaths = [mainBundle pathsForResourcesOfType:@"png" inDirectory:nil];
    images = [[NSMutableArray alloc] init];
    int mycount = [imagePaths count];
    int i;
    for(i=0;i<mycount;i++)
    {
        NSImage * image = [[NSImage alloc] initWithContentsOfFile:[imagePaths objectAtIndex:i]];
        [images addObject:image];
        [image release];
    }
    count = 0;
    [imageview setImage:[images objectAtIndex:count]];
}
-(IBAction)nextImage:(id)sender
{
    count++;
    if (count==[images count]) {
        count=0;
    }
    [imageview setImage:[images objectAtIndex:count]];
}
*/
//
-(IBAction)nextImage:(id)sender
{
    count ++;
    if(count == [imagePaths count])
    {
        count = 0;
    }
    NSImage * image = [[NSImage alloc] initWithContentsOfFile:[imagePaths objectAtIndex:count]];
    
    [imageview setImage:image];
    [image release];
}
-(IBAction)beforeImage:(id)sender
{
    count --;
    if(count < 0)
    {
        count = count + (int)[imagePaths count];
    }
    NSImage * image = [[NSImage alloc] initWithContentsOfFile:[imagePaths objectAtIndex:count]];
    
    [imageview setImage:image];
    [image release];
}
//加一个输入框,输入数字,查看特定序号的图片
-(IBAction)numImage:(id)sender
{
    count=[num intValue];
    if((count < 0)||(count > [imagePaths count]))
    {
        exit(-1);
    }
    NSImage * image = [[NSImage alloc] initWithContentsOfFile:[imagePaths objectAtIndex:count]];
    
    [imageview setImage:image];
    [image release];
}
//
@end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值