gpt4 book ai didi

cocoa - 无法更改桌面图像,因为 url 不是文件 url

转载 作者:行者123 更新时间:2023-12-03 17:05:21 25 4
gpt4 key购买 nike

我正在尝试使用以下代码更改桌面图像:

NSError *anError = nil;
NSString *imageURLString = [[NSBundle mainBundle] pathForResource:@"wallpaper" ofType:@"jpg"];
NSURL *imageURL = [NSURL URLWithString:imageURLString];

[[NSWorkspace sharedWorkspace] setDesktopImageURL:imageURL forScreen:[NSScreen mainScreen] options:nil error:&anError];

但它总是在控制台中抛出此异常消息:

Invalid parameter not satisfying: url != nil && [url isFileURL]

当我尝试记录 - [url isFileURL] 时,它返回 NO,我无法弄清楚。

当我尝试打印 imageURL 的描述时,它打印了以下消息:

<CFURL 0x100165c50 [0x7fff7f508ea0]>{type = 15, string = /Users/miraaj/Library/Developer/Xcode/DerivedData/DesktopFun-cevkcaebvcfnstgqbulqyibcfvru/Build/Products/Debug/DesktopFun.app/Contents/Resources/wallpaper.jpg, encoding = 134217984, base = (null)}

谁能告诉我这个问题的原因吗?

最佳答案

您使用了错误的方法来创建 NSURL。你想要

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"wallpaper" ofType:@"jpg"];
NSURL* imageURL = [NSURL fileURLWithPath:imagePath isDirectory:NO];

或者,您可以保存一个步骤并直接从 NSBundle 获取 NSURL(在 10.6 及更高版本上):

NSURL* imageURL = [[NSBundle mainBundle] URLForResource:@"wallpaper" withExtension:@"jpg"];

关于cocoa - 无法更改桌面图像,因为 url 不是文件 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9858858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com