0

I am trying to get the NSData from a file path but I keep getting an error that the file couldn't be opened. Any ideas how to get the path so I can get the NSData.

for (int t = 0; t < [self.myarray count]; t++) {
    NSURL *url1 = [NSURL fileURLWithPath:[self.myarray objectAtIndex:t]];
    NSLog (@"url1: %@", url1);
    NSError* error = nil;

    NSString *filePath = [url1 path];
    NSData* data = [NSData dataWithContentsOfFile:filePath options:NSDataReadingUncached error:&error];
    if (error) {
        NSLog(@"Data Error: %@", [error localizedDescription]);

    } else {
        NSLog(@"Data is: %@", data);

    }  

}

Here's the error log

url1: file:///var/mobile/Containers/Data/Application/85E795CA-B31D-4BD7-A777-DE7B392D6762/Documents/pImw.mp4

Data Error: The file “pImw.mp4” couldn’t be opened because there is no such file.

Marc Matteo
  • 103
  • 2
  • 9
  • What does `myarray` contain? Consider that the absolute path to the documents folder changes periodically. And use `dataWithContentsOfURL` API to get rid of the unnecessary step `[url1 path]`. And the second `else` is never reached. – vadian Aug 07 '18 at 17:19
  • Are you getting the document directory path programmatically? If not, have a look at this question https://stackoverflow.com/questions/24055146/how-to-find-nsdocumentdirectory-in-swift. Scroll down beyond the accepted answer for more recent versions of swift. – Nick Aug 07 '18 at 17:26
  • The second "else" was an error when I was making this post. Sorry. The "myarray" is a string from documents directory. NSString *finalVideoURLString = [documentsDirectory stringByAppendingPathComponent: – Marc Matteo Aug 07 '18 at 17:56

0 Answers0