【问题标题】:load images into ikimagebrowser from a folder?将图像从文件夹加载到 ikimagebrowser?
【发布时间】: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


【解决方案1】:

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

在该协议中,需要三种方法:

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

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

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

更多信息:

【讨论】:

  • 我有一组 NSImages 已经在一个数组中。我用什么来满足数据源协议要求?谢谢
  • 忘了说 - 这些是从网上直接下载到可变数组中的图像。
  • @DavidDelMonte:创建一个符合协议的类,并将每个图像包装在它的实例中。
  • 谢谢。我正在学习..我已经制作了课程(MyImageObject。我只需要弄清楚如何将图像包装到其中。
  • @DavidDelMonte:制作包装器并没有什么特别之处。包装器只是一个对象,它包含一个对象并代表它提供其他功能(例如,作为浏览器项)。您所要做的就是创建一个属性来保存图像,然后实现协议。
猜你喜欢
  • 1970-01-01
  • 2012-11-15
  • 2012-07-28
  • 2012-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-16
相关资源
最近更新 更多