Liban Abdulle
Liban Abdulle

Reputation: 177

NSURL object from initWithString gives -[NSURL length] error

I'm trying to find a file with a path, then delete it using the NSFileManager class. the [info objectForKey:UIImagePickerControllerMediaURL] does return a string, so I dont' understand why its failing on a valid parameter.

NSError *error;

NSFileManager *manager = [NSFileManager defaultManager];

NSURL *url = [[NSURL alloc] initWithString:[info
              objectForKey:UIImagePickerControllerMediaURL]];


if ([manager isDeletableFileAtPath: [info 
                  objectForKey:UIImagePickerControllerMediaURL]]) {
    BOOL success = [manager removeItemAtURL:url error:&error];
    if (!success) {
        NSLog(@"Error removing file at path: %@", error.localizedDescription);
    }
}

And I get this error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException'
   , reason: '-[NSURL length]: unrecognized selector sent to instance 0x175ede10'

Upvotes: 1

Views: 6854

Answers (5)

Marc
Marc

Reputation: 6190

The documentation says for UIImagePickerControllerMediaURL:

Specifies the filesystem URL for the movie. The value for this key is an NSURL object.

And your error message says the same:

Therefore

NSError *error;
NSFileManager *manager = [NSFileManager defaultManager];
NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];

if ([manager isDeletableFileAtPath:url]) {
    BOOL success = [manager removeItemAtURL:url error:&error];
    if (!success) {
        NSLog(@"Error removing file at path:%@", error.localizedDescription);
    }
}

Upvotes: 3

Ashvin
Ashvin

Reputation: 9027

May be help you.

Try this

NSString *urlStr =[NSString stringWithFormat:@"%@", [info
              objectForKey:UIImagePickerControllerMediaURL]];
NSURL *url = [[NSURL alloc] URLWithString:urlStr];

than you are directly assigning URL

NSURL *url = [[NSURL alloc] initWithString:[info
              objectForKey:UIImagePickerControllerMediaURL]];

Upvotes: 2

Hussain Shabbir
Hussain Shabbir

Reputation: 15035

First check that your objectforkey value what it is returning. If it is returning url then in your code third line which you have written initWithString just replace to initWithUrl and try.

Upvotes: 0

Nirav Gadhiya
Nirav Gadhiya

Reputation: 6342

Replace following line

NSURL *url = [[NSURL alloc] initWithString:[info
              objectForKey:UIImagePickerControllerMediaURL]];

with this line

NSURL * url = [NSURL fileURLWithPath:[info
              objectForKey:UIImagePickerControllerMediaURL]];

i am not sure that it will solve your problem. But i feel that it should solve your problem because for file manager URLWithString: is not working as the string contains file path so you have to use fileURLWithPath:

Upvotes: 0

Ashwani
Ashwani

Reputation: 3491

Basically you are not getting the URL string in dictionary because [info objectForKey:UIImagePickerControllerMediaURL] don't provide url for picked image.

Documentaton says:

The Assets Library URL for the original version of the picked item. After the user edits a picked item—such as by cropping an image or trimming a movie—the URL continues to point to the original version of the picked item.

But you can get the image as UIImage like:

UIImage *image = info[UIImagePickerControllerOriginalImage];

Upvotes: 0

Related Questions