无法识别的选择器发送到实例

时间:2011-11-25 02:22:32

标签: iphone objective-c xcode uiimage

我遇到了一个问题,即使在阅读了几个帖子后我也听不懂

这是我得到的错误:

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

这是代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *filePath = [[self documentsPath] stringByAppendingPathComponent:@""];

    NSFileManager *imagesFileManager = [NSFileManager defaultManager];

    imagesArr = [[imagesFileManager contentsOfDirectoryAtPath:filePath error:nil]filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.jpg'"]];

    arryList = [[NSMutableArray alloc] initWithArray:[imagesArr copy]];//display text
    imagesList = [[NSMutableArray alloc]init];//display image

    NSString *docPath = [self documentsPath];

    for (NSString *anImagePath in arryList) {
        anImagePath = [NSString stringWithFormat:@"%@/%@",docPath,anImagePath];
        UIImage *image = [UIImage imageWithContentsOfFile:anImagePath];
        if (image)
            [imagesList addObject:image];

        NSLog(@"%@", anImagePath);
    }
}   

2 个答案:

答案 0 :(得分:6)

您不是length UIImage上的forinarrayList循环。这是因为NSString *anImagePath中的对象实际上是图像,但是循环认为它们是字符串,因为您基本上使用for (NSString *anImagePath in arryList) { anImagePath = [NSString stringWithFormat:@"%@/%@",docPath,anImagePath]; UIImage *image = [UIImage imageWithContentsOfFile:anImagePath]; if (image) [imagesList addObject:image]; NSLog(@"%@", anImagePath); } 对它们进行了类型转换。尝试更改您的代码:

for (UIImage *image in arryList) {
    if (image)
        [imagesList addObject:image];

    NSLog(@"%@", anImagePath);
}

到此:

UIImage

我猜你已经在数组中存储了{{1}}个对象。如需更多参考,请访问Trying to display array of images, code returns iphone to home screen

答案 1 :(得分:0)

好的,所以这是另一回事。希望这次我能更好地理解你的问题。当您尝试修改其集合时,forin会出现问题,因此这样做可能会有所帮助。

改变这个:

for (NSString *anImagePath in arryList) {
    anImagePath = [NSString stringWithFormat:@"%@/%@",docPath,anImagePath];

对此:

for (NSString *anImagePath in imagesArr) {
    [arrayList setObject:[NSString stringWithFormat:@"%@/%@",docPath,anImagePath]
                 atIndex:[imagesArr indexOfObject:anImagePath]];

这样,我们循环遍历我们修改的数组的副本,而不是我们自己修改的数组。但是,这是一个相当糟糕的代码(将arrayList创建为imagesArr的副本,然后删除其所有对象),因此我们可以尝试以下代码。

改变这个:

arrayList = [[NSMutableArray alloc] initWithArray:[imagesArr copy]];

对此:

arrayList = [[NSMutableArray alloc] init];

然后在我们的forin循环中,更改此内容:

for (NSString *anImagePath in imagesArr) {
    [arrayList setObject:[NSString stringWithFormat:@"%@/%@",docPath,anImagePath]
                 atIndex:[imagesArr indexOfObject:anImagePath]];

对此:

for (NSString *anImagePath in imagesArr) {
    [arrayList addObject:[NSString stringWithFormat:@"%@/%@",docPath,anImagePath]];

希望这更好用!