我有一些jpg图像,我从相机镜头保存到文档目录。我想找到文档目录中的所有jpg图像,并将它们加载到一个数组中,这样它们就可以处于溢出视图中。
这是我的代码,用于在文档目录中查找和排序jpg图像。
NSString *extension = @"jpg";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSMutableArray *jpegFiles = [NSMutableArray arrayWithCapacity: [contents count]];
NSString *filename;
for (filename in contents)
{
if ([[filename pathExtension] isEqualToString:extension])
{
[jpegFiles addObject:[UIImage imageNamed:[documentsDirectory stringByAppendingPathComponent:filename]]];
}
}
// Add the coverflow view
coverflow = [[TKCoverflowView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
coverflow.coverflowDelegate = self;
coverflow.dataSource = self;
[self.view addSubview:coverflow];
[coverflow setNumberOfCovers:6];
- (void) coverflowView:(TKCoverflowView*)coverflowView coverAtIndexWasBroughtToFront:(int)index{
NSLog(@"Front %d",index);
}
- (TKCoverflowCoverView*) coverflowView:(TKCoverflowView*)coverflowView coverAtIndex:(int)index{
TKCoverflowCoverView *cover = [coverflowView dequeueReusableCoverView];
if(cover == nil){
// Change the covers size here
cover = [[TKCoverflowCoverView alloc] initWithFrame:CGRectMake(0, 0, 280, 210)]; // 224
cover.baseline = 200;
}
cover.image = [covers objectAtIndex:index % [covers count]];
return cover;
}
我已经尝试了所有我能想到的东西,没有任何作用。
有人建议我通过从路径名数组中获取图像的路径来加载每个图像,并使用imageWithContentsOfFile方法加载图像。
我已经看了这个,并搜索了如何做到但没有运气。有人可以告诉我如何做到这一点。
答案 0 :(得分:2)
你应该按照以下方式进行,
if ([[filename pathExtension] isEqualToString:extension])
{
[jpegFiles addObject:[UIImage imageWithContentsOfFile:[documentsDirectory stringByAppendingPathComponent:filename]]];
}