0

我有两个数组,一个代表全尺寸图像列表,另一个代表图像的缩略图。有一种方法,使用 NSPredicate 来检查全尺寸图像是否有缩略图?

拇指被称为 img{number}_thumb.jpg,全尺寸图像被称为 img{number}.jpg。

4

3 回答 3

1

使用数组、字符串和循环

NSArray *thumbs=@[@"img1_thumb.jpg",@"img2_thumb.jpg",@"img3_thumb.jpg",@"img4_thumb.jpg",@"img5_thumb.jpg",];
NSArray *images=@[@"img1",@"img2",@"img3",@"img41",@"img5"];

BOOL isSame=YES;
for (NSString *name in images) {
    if (![thumbs containsObject:[NSString stringWithFormat:@"%@_thumb.jpg",name]]) {
        isSame=NO;
        NSLog(@"%@ doesn't has thumb image",name);
        break; //if first not found is not good enough remove this break
    }
}
NSLog(@"%@",isSame?@"All thumb has image":@"All thumb does not have image");

使用 NSPredicate:

for (NSString *image in images) {
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF like [c]%@",[NSString stringWithFormat:@"%@_thumb.jpg",image]];
    NSArray *filtered=[thumbs filteredArrayUsingPredicate:predicate];
    if (filtered.count==0) {
        NSLog(@"%@ not found",image);
    }
}
于 2013-04-01T12:29:27.723 回答
1

您可以使用indexOfObjectsPassingTest:

NSArray *imageThumbs=   [NSArray arrayWithObjects:@"img1_thumb.jpg",@"img2_thumb.jpg",@"img3_thumb.jpg",@"img4_thumb.jpg",nil];
    NSArray *images=[NSArray arrayWithObjects: @"img1",@"img2",@"img3",@"img4",@"img5",nil];
    for(NSString *image in images)
    {
        if ([imageThumbs HasPrefix:image]) {
            NSLog(@"has thumbnail %@",image);
        }
    }  

@interface NSArray (fileterArrayUsingBlocks)
-(BOOL)HasPrefix : (NSString *)path;
@end
@implementation NSArray (fileterArrayUsingBlocks)

-(BOOL)HasPrefix : (NSString *)path
{
    NSIndexSet  *lIndexSet = [self indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj hasPrefix:path]) {
            *stop = YES;  
            return YES;
        } else
            return NO;
    }];
    if (![lIndexSet count])
        return NO;
    return YES;
}

@end
于 2013-04-01T12:51:00.660 回答
0

如果可以安排两个数组具有相同的值(即 img1.jpg 和 img1_thumb.jpg 都用“1”表示,但在不同的数组中),那么没有缩略图的图像集是:

[[NSMutableSet setWithArray:images] minusSet:[NSSet setWithArray:thumbnails]]
于 2013-04-03T02:55:04.780 回答