2013-01-14 42 views
0

我新的可可和我有一個IKImageBrowserView,這就是我如何加載圖像到它:從文件夾將圖像加載到ikimagebrowser中?

- (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的文件夾加載具有不同名稱的圖像?

所以目前我按照名稱澳大利亞1,澳大利亞2,澳大利亞3,澳大利亞4等等加載圖片。 如何從包文件夾加載圖像?

+0

我不知道爲什麼你這個標記爲的iOS。 Cocoa和Image Kit只在OS X上可用。 –

+0

目前還不清楚你正在嘗試做什麼或者發生了什麼。你的意思是「我的圖片是按名稱加載的 - @」澳大利亞「」? 「你的照片需要有相同的名字和數字」是什麼意思? 「從文件夾加載具有不同名稱的圖像」與您在此做的有何不同? –

+0

我不認爲我把它標記爲可可...好吧,無論如何,此刻我的圖像被加載成一個循環的名字,所以我得到了名爲「澳大利亞1,澳大利亞2,澳大利亞3 ...」等圖像。如何從包文件夾加載圖像? – Rokas

回答

1

您的數據源需要將項目返回到符合IKImageBrowserItem協議的圖像瀏覽器視圖。你的myImageObject類是一個很好的開始。

在該協議中,需要三種方法:

首先,我只是使用你已經給每個myImageObject的路徑。您可以將其用作標識符字符串和圖像表示。

根據你在這個應用程序中做了什麼,你可能會發現它有利於以後,由於內存和/或速度的原因,你自己加載每個圖像。如果在分析後得出這個結論,你可以自己加載圖像爲NSImage或CGImage,適當地改變你的表示類型,並返回圖像作爲表示。

作爲數據源,您將在_importedImages陣列中返回the number of items,並且在詢問the item at an index時,通過objectAtIndex:返回該項目。

更多信息:

+0

我有一組NSImages已經在一個數組中。我該如何使用數據源協議要求?謝謝 –

+0

忘了說 - 這些是從網上直接下載到可變數組的圖像。 –

+0

@DavidDelMonte:創建一個符合協議的類,並將每個圖像包裝在一個實例中。 –

相關問題