gpt4 book ai didi

objective-c - 将图像从文件夹加载到 ikimagebrowser 中?

转载 作者:行者123 更新时间:2023-12-03 17:55:32 25 4
gpt4 key购买 nike

我是 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];
}

现在我的图像按名称加载 - @“澳大利亚”。这有点不方便,因为您的图像需要具有相同的名称和编号。如何从已导入 xcode 的文件夹中加载不同名称的图像?

所以目前我正在加载名称为 Australia1、Australia2、Australia3、Australia4... 等的图像。如何从捆绑文件夹加载图像?

最佳答案

您的数据源需要将符合 IKImageBrowserItem 协议(protocol)的项目返回到图像浏览器 View 。您的 myImageObject 类是一个很好的起点。

在该协议(protocol)中,需要三个方法:

首先,我只使用您已经为每个 myImageObject 提供的路径。您可以将其用作标识符字符串和图像表示。

根据您在此应用程序中执行的其他操作,您稍后可能会发现出于内存和/或速度原因,自行加载每个图像是有利的。如果在分析之后您确实得出了这个结论,您可以自己将图像加载为 NSImage 或 CGImage,适本地更改您的表示类型,然后返回图像作为表示。

作为数据源,您将返回 the number of items在您的 _importedImages 数组中,并且当要求 the item at an index 时,通过 objectAtIndex: 返回该项目。

更多信息:

关于objective-c - 将图像从文件夹加载到 ikimagebrowser 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14320580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com