0

在我下面的代码中,

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”

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

4

2 回答 2

2

您正在fileurl错误地构建您的。

替换这一行:

NSURL *fileurl=[NSURL URLWithString:localPath];

有了这个:

NSURL *fileurl=[NSURL fileURLWithPath:localPath];

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

于 2016-01-12T08:02:30.567 回答
0

我认为您的字符串将以 file:// 开头,您可以尝试将其更改为以下内容吗:

在斯威夫特:

CKAsset(fileURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("temporary", ofType: "png")!))
于 2016-01-11T08:51:13.183 回答