UIImagePickerControllerMediaURL

UIImagePickerControllerMediaURL

本文介绍了来自initWithString的NSURL对象给出-[NSURL长度]错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找具有路径的文件,然后使用NSFileManager类将其删除。 [info objectForKey:UIImagePickerControllerMediaURL] 确实返回了一个字符串,所以我不明白为什么它在有效参数上失败。

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

我收到此错误:

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


推荐答案

文档说明 UIImagePickerControllerMediaURL

您的错误消息也是如此:

And your error message says the same:

因此

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

这篇关于来自initWithString的NSURL对象给出-[NSURL长度]错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 21:48