0

in my Code below,

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"];
}

error will occur in the line of "CKAsset *myfile=[[CKAsset alloc] initWithFileURL:fileurl];" error message is below:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Non-file URL'

I don't know how to solve this and search web all night long, but no help. Please help me ! I am a code beginner !

prasun
  • 7,073
  • 9
  • 41
  • 59
avocet
  • 1

2 Answers2

2

You are building your fileurl incorrectly.

Replace this line:

NSURL *fileurl=[NSURL URLWithString:localPath];

with this one:

NSURL *fileurl=[NSURL fileURLWithPath:localPath];

Always use the fileURLWithPath: method to create a file URL from a file path. Use URLWithString: when creating a URL for strings that represent a proper URL starting with a scheme (such as http:, mailto:, etc.).

rmaddy
  • 314,917
  • 42
  • 532
  • 579
0

I think your string will start with file:// Could you try changing it to something like this:

In Swift:

CKAsset(fileURL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("temporary", ofType: "png")!))
Edwin Vermeer
  • 13,017
  • 2
  • 34
  • 58