Cocoa Hello World Screensaver

技术标签: Objective-C.  可可  屏幕保护程序

我一直在学习NSView,因此我以为我会在屏幕保护程序中拍摄。我已经能够在NSView中显示和图像,但我无法看到修改此示例代码以在屏幕虚拟中显示一个简单的图片。

http://www.mactech.com/articles/mactech/vol.20/20.06/screensaversincocoa/

BTW伟大的教程与雪豹合作。

我想只是展示一个图像,我需要一些看起来像这样的东西......

我究竟做错了什么?

//
//  try_screensaverView.m
//  try screensaver
//

#import "try_screensaverView.h"

@implementation try_screensaverView

- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview
{
    self = [super initWithFrame:frame isPreview:isPreview];
    if (self) {
        [self setAnimationTimeInterval:1]; //refresh once per sec
    }
    return self;
}

- (void)startAnimation
{
    [super startAnimation];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"leaf" ofType:@"JPG" inDirectory:@""];
    image = [[NSImage alloc] initWithContentsOfFile:path];
}

- (void)stopAnimation
{
    [super stopAnimation];
}

- (void)drawRect:(NSRect)rect
{
    [super drawRect:rect];
}

- (void)animateOneFrame
{
    //////////////////////////////////////////////////////////
    //load image and display  This does not scale the image

    NSRect bounds = [self bounds];
    NSSize newSize;
    newSize.width = bounds.size.width;
    newSize.height = bounds.size.height;
    [image setSize:newSize];
    NSRect imageRect;
    imageRect.origin = NSZeroPoint;
    imageRect.size = [image size];
    NSRect drawingRect = imageRect;
    [image drawInRect:drawingRect fromRect:imageRect operation:NSCompositeSourceOver fraction:1];
}

- (BOOL)hasConfigureSheet
{
    return NO;
}

- (NSWindow*)configureSheet
{
    return nil;
}

@end

看答案

NSRect bounds = [self bounds];
NSSize newSize;
newSize.width = bounds.size.width;
newSize.height = bounds.size.height;
[image setSize:newSize];

我不知道为什么你这样做。

NSRect imageRect;
imageRect.origin = NSZeroPoint;
imageRect.size = [image size];

A.K.A. [self bounds].size.

NSRect drawingRect = imageRect;
[image drawInRect:drawingRect fromRect:imageRect operation:NSCompositeSourceOver fraction:1];

IE。, [image drawInRect:[self bounds] fromRect:[self bounds] operation:NSCompositeSourceOver fraction:1].

如果您尝试在其自然大小绘制图像,则没有理由发送A. setSize: 信息。剪掉了整个第一部分,其余的应该很好。

如果您正在尝试填充屏幕(这将是缩放,那将与评论相矛盾),请设置 drawingRect[self bounds], 不是 imageRect。这完全按照读数:

image,
    draw into (the bounds of the view),
    from (the image's entire area).

[image
    drawInRect:[self bounds]
      fromRect:imageRect
              ⋮
];

既不是自然尺寸固定位置绘制也不是全屏绘制是一个有效的屏幕保护程序。后者是不可挽回的;您可以使前者通过在屏幕周围进行动画来进行动画。


智能推荐

hello,world

hello,world 使用notepad++编写源代码 找到java文件所在目录,cmd打开命令行窗口, 编译java文件:javac+空格+文件名.java,生成class文件 运行class文件 可能遇到的问题 单词大小写不能出现问题,java大小敏感 尽量使用英文 文件名和类名必须保持一致,首字母大写 符号使用中文 命令行运行如果出现找不到或无法加载主类,使用java+包名+类型名的方式运...

Hello World !

今天算是正式敲了自己的第一个代码 入门级。。。emmmmmmmm,说自己入门级仿佛是在侮辱这个词,算是连门都没入,可能是个人都听说过hello world的代码,可怜巴巴的只能敲出来个这。 #include <stdio.h> int main() { printf(“Hello World !\n”); return 0; } 虽然是个最简单的代码,但好歹不负...

场效应管放大电路

金属-氧化物-半导体(MOS)场效应管 N沟道增强型MOSFET 栅源加电压,在电场作用下产生沟道。产生沟道的门限开启电压VT。 漏源加电压,产生电压梯度,导致沟道夹断。预夹断的临界条件 输出特性 特性方程 可变电阻区                         &...

【响应式】foundation栅格布局的“尝鲜”与“填坑”

  提到响应式,就不得不提两个响应式框架——bootstrap和foundation。在标题上我已经说明白啦,今天给大家介绍的是foundation框架。 何为“尝鲜”?就是带大伙初步一下foundation的灵活和强大 何为“踩坑”?就是我把我使用的时候踩过的坑给标个记号,这样大伙用的时候就可以“绕道而...

word2vec笔记

word2vec 词向量 one hot Distributed representation CBOW&Skip-Gram CBOW Skip-Gram sigmoid函数 Huffman树 基于Hierarchical Softmax的模型 基于Negative Sampling的模型 本文基于word2vec原理CBOW与Skip-Gram模型基础 CBOW与Skip-Gram的模型...

猜你喜欢

2021-03-14

官网:https://router.vuejs.org/zh/guide/essentials/navigation.html 一、安装路由 npm install vue-router --save-dev 在src目录下创建router目录 使用 vuecli3创建项目选择路由会自动创建route目录 在main.js引入router 二、配置路由 1、 hash: 使用 URL hash 值...

文献阅读-ICRA2020-精确的实时数据驱动的对RAVEN-II手术机器人末端执行器位置估计器

文章序号、所属单元及链接:1646-Surgical Robotics Laparascopy 一作所属单位:University of Washington 读后体会:以我浅薄的学术认知原本认为这篇论文挺牛逼的,实验细节给的很多,关键是结果提升很大。跟老师交流之后才知道这论文只能骗骗外行人,关键的问题是他是空载进行实验的,绳驱动机器人负载和空载的差距很大,还需要考虑绳经过一段时间使用之后动态特性...

RoarCTF 2019Easy Calc表达式注入

[RoarCTF 2019]Easy Calc表达式注入 - 幕布 接着发现了api的接口地点calc.php和get的参数 直接url后面加上calc.php显示了一部分源码 可以发现过滤了很多的东西,并且计算器的页面上是只允许输入数字的,所以可以用带空格的数字绕过(用? num代替?num) scandir("/")扫一下根目录,"/"被waf过滤了,单...

一文弄懂ARQ协议与Nagle算法

本文参考文献: 1、ARQ-维基百科 https://zh.wikipedia.org/wiki/ARQ 2、TCP/IP(三) —— 可靠传输工作原理 http://pmghong.blog.51cto.com/3221425/1242470 3、TCP可靠传输&流量控制&拥塞控制  http://my.oschina.net...

centos6.5 安装rabbitmq

楼主安装在远程centos服务器上: 1.操作环境 centos6.5 2.Erlang下载 RabbitMQ是使用Erlang开发,所以安装RabbitMQ前需要先安装Erlang wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm 3.Erlang安装 rpm -ivh epel...

问答精选

SQL, update command not ending properly

It keeps saying : ORA-00933: SQL command not properly ended Pls help me or give me a link to a solution You can use a correlated subquery instead:...

How can I escape $.each loop with my data?

I'm doing an Json call to retrieve an a list of locations with information details for each location. longitude and latitude are included in this info. I am using Google's distance matrix api to get t...

How to display all the columns (and their type) in all tables of all schemas in a database?

Suppose you have a database which has an 'n' number of schemas with an 'n' number of tables each. Each of these contain an 'n' number of columns. How would I print all this data along with the data ty...

How to set the java.library.path in intelliJ Idea

Could anyone please help how do I solve this error: I am using IDEA IDE as a first time, and have been using Resin_4.0.37 as a server to test my work. As soon as I start my lcoal server in debug mode ...

How to calculate mouse coordinate based on resolution c#

i am trying to develop a remote desktop apps with c#. so i have couple of question regarding mouse coordinate calculation based on picture box suppose i have picture box and i want to capture mouse co...

相关问题

相关文章

热门文章

推荐文章

相关标签

推荐问答