如何在自定义视图控件中查看连续图像

时间:2016-04-22 07:31:28

标签: objective-c xcode cocoa nsimage cgcolorspace

我正在做一个实时视频应用程序。直到现在我解决了连续发送图像(帧)的问题,而且我还可以在另一台笔记本电脑上接收这些图像。但是如何查看图像中的图像。自定义视图控件,使其看起来像一个视频? 换句话说,我有很多NSImage对象,我想像视频一样查看它们。 使用CGColorSpaceRef?我是Mac OS X的可可编程新手。 任何人都可以给我一些代码吗?

1 个答案:

答案 0 :(得分:0)

#import "ViewController.h"
#import <Quartz/Quartz.h>

@interface ViewController ()
{
    NSArray *activityAnimationImages;
    IBOutlet NSImageView *activityImageView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

// Do any additional setup after loading the view.

NSMutableArray *array = [NSMutableArray array];

for (NSUInteger i = 0; i<=29; i++)
{
    //Preloader is a set of images
    NSImage *image = [NSImage imageNamed:[NSString stringWithFormat:@"Preloader_10_000%02lu",(unsigned long)i]];
    [array addObject:image];
}

activityAnimationImages = [NSArray arrayWithArray:array];

//Create layer
CALayer *layer = [CALayer layer];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
[animation setCalculationMode:kCAAnimationDiscrete];
[animation setDuration:1.0f];
[animation setRepeatCount:HUGE_VALF];
[animation setValues:activityAnimationImages];

[layer setFrame:NSMakeRect(0, 0, 100, 100)];
layer.bounds = NSMakeRect(0, 0, 100, 100);
[layer addAnimation:animation forKey:@"contents"];

[activityImageView setLayer:[CALayer layer]];
[activityImageView setWantsLayer:YES];
[activityImageView.layer addSublayer:layer];
}