我有两个数组,一个表示全尺寸图像列表,另一个表示图像的缩略图。有一种方法,使用NSPredicate,检查一个完整尺寸的图像是否有缩略图?
拇指名为img {number} _thumb.jpg,全尺寸图片名为img {number} .jpg。
答案 0 :(得分: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);
}
}
答案 1 :(得分:1)
您可以使用indexesOfObjectsPassingTest:
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
答案 2 :(得分:0)
如果你可以安排两个数组具有相同的值(即img1.jpg和img1_thumb.jpg都用“1”表示,但在不同的数组中),那么没有缩略图的图像集是:
[[NSMutableSet setWithArray:images] minusSet:[NSSet setWithArray:thumbnails]]