NSURL object from initWithString gives -[NSURL length] error

爱⌒轻易说出口 提交于 2019-12-13 01:13:43

问题


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'

回答1:


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);
    }
}



回答2:


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



回答3:


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



回答4:


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:




回答5:


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.



来源:https://stackoverflow.com/questions/19724028/nsurl-object-from-initwithstring-gives-nsurl-length-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!