【发布时间】:2013-01-15 09:56:23
【问题描述】:
我是 cocoa 的新手,我有一个 IKImageBrowserView,这就是我将图像加载到它的方式:
- (IBAction)loadImages:(id)sender
{
NSMutableArray *urls = [[NSMutableArray alloc]init];
int i = 1;
for (i=1; i<55; i++) {
NSString *photoNumber = [NSString stringWithFormat:@"%i", i];
NSMutableString *urlString = [[NSMutableString alloc] initWithString:@"Australia"];
[urlString appendString:photoNumber];
NSURL* url = [[NSBundle mainBundle] URLForImageResource:urlString];
[urls addObject:url];
}
[self addImagesWithPaths:urls];
}
- (void)addAnImageWithPath:(NSString *)path
{
myImageObject *p;
/* add a path to our temporary array */
p = [[myImageObject alloc] init];
[p setPath:path];
[_importedImages addObject:p];
}
- (void)addImagesWithPath:(NSString *)path recursive:(BOOL)recursive
{
NSInteger i, n;
BOOL dir;
[[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&dir];
if (dir)
{
NSArray *content = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
n = [content count];
// parse the directory content
for (i=0; i<n; i++)
{
if (recursive)
[self addImagesWithPath:[path stringByAppendingPathComponent:[content objectAtIndex:i]] recursive:YES];
else
[self addAnImageWithPath:[path stringByAppendingPathComponent:[content objectAtIndex:i]]];
}
}
else
{
[self addAnImageWithPath:path];
}
}
/* performed in an independant thread, parse all paths in "paths" and add these paths in our temporary array */
- (void)addImagesWithPaths:(NSArray *)urls
{
NSInteger i, n;
n = [urls count];
for ( i= 0; i < n; i++)
{
NSURL *url = [urls objectAtIndex:i];
[self addImagesWithPath:[url path] recursive:NO];
}
/* update the datasource in the main thread */
[self performSelectorOnMainThread:@selector(updateDatasource) withObject:nil waitUntilDone:YES];
}
现在我的图片按名称加载 - @"Australia"。这有点不方便,因为您的图像需要具有相同的名称和编号。如何从已导入 xcode 的文件夹中加载不同名称的图像?
所以目前我正在加载名称为澳大利亚 1、澳大利亚 2、澳大利亚 3、澳大利亚 4 的图像......等等。 如何从捆绑文件夹中加载图像?
【问题讨论】:
-
我不知道您为什么将其标记为 iOS。 Cocoa 和 Image Kit 仅适用于 OS X。
-
不清楚您要做什么或发生了什么。 “我的图片是按名称加载的 - @"Australia"”是什么意思? “您的图像需要具有相同的名称和编号”是什么意思? “从文件夹中加载不同名称的图像”与您在此处执行的操作有何不同?
-
我不认为我将其标记为可可...好吧,无论如何,目前我的图像按名称循环加载,所以我得到了名为“Australia1,Australia2,Australia3.. 。“ 等等。如何从捆绑文件夹中加载图像?
标签: objective-c macos cocoa ikimagebrowserview imagekit