首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xcode: CKAsset in CloudKit

Xcode: CKAsset in CloudKit
EN

Stack Overflow用户
提问于 2016-01-10 03:56:56
回答 2查看 489关注 0票数 0

在我下面的代码中,

代码语言:javascript
复制
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *localPath = [[paths objectAtIndex: 0] stringByAppendingPathComponent: @"temporary.png"];
    NSLog(@"local path=%@", localPath);
    [UIImagePNGRepresentation(self.scriptPicture.image) writeToFile:localPath atomically:YES];
    //[opData.scripts writeToFile:localPath atomically:YES];
    if ([[NSFileManager defaultManager] fileExistsAtPath:localPath]) {
        NSLog(@"file is exist");
        NSURL *fileurl=[NSURL URLWithString:localPath];
        CKAsset *myfile=[[CKAsset alloc] initWithFileURL:fileurl];
        //postRecrod[@"scriptPicture"]=myfile;
        [postRecrod setObject:myfile forKey:@"scriptPicture"];
}

错误将发生在"CKAsset *myfile=[[CKAsset alloc] initWithFileURL:fileurl];“错误消息如下:

由于“NSInvalidArgumentException”异常终止应用程序,原因:“非文件URL”

我不知道如何解决这个问题,整夜搜索网页,但没有帮助。请救救我!我是个代码初学者!

EN

回答 2

Stack Overflow用户

发布于 2016-01-12 08:02:30

您正在错误地构建fileurl

替换这一行:

代码语言:javascript
复制
NSURL *fileurl=[NSURL URLWithString:localPath];

有了这个:

代码语言:javascript
复制
NSURL *fileurl=[NSURL fileURLWithPath:localPath];

始终使用fileURLWithPath:方法从文件路径创建文件URL。在为以方案开头的字符串创建URL时使用URLWithString: (如http:、mailto:等)。

票数 2
EN

Stack Overflow用户

发布于 2016-01-11 08:51:13

我认为您的字符串将以file://开始,您是否可以尝试将其更改为如下所示:

在Swift:

代码语言:javascript
复制
CKAsset(fileURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("temporary", ofType: "png")!))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34701898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档