0
在我製作的應用程序中,我需要能夠在tableView(_:)
函數已創建單元格之後將圖像加載到表格單元格。我發現這可以通過線程來完成,但我不完全確定它是如何完成的。在swift中以表格視圖加載圖像加載
有沒有人知道我可以如何在tableView(_:)
加載其他單元格後啓動單獨的線程,以便將圖像添加到單元格中,同時保持表格視圖中的平滑滾動。
在我製作的應用程序中,我需要能夠在tableView(_:)
函數已創建單元格之後將圖像加載到表格單元格。我發現這可以通過線程來完成,但我不完全確定它是如何完成的。在swift中以表格視圖加載圖像加載
有沒有人知道我可以如何在tableView(_:)
加載其他單元格後啓動單獨的線程,以便將圖像添加到單元格中,同時保持表格視圖中的平滑滾動。
這裏是你的研究
http://www.raywenderlich.com/76341/use-nsoperation-nsoperationqueue-swift
檢查本教程很好的起點,它展示瞭如何使用的NSOperation實現自己的目標
我做了這樣裝載機前面,在OBJ-C,但你可以輕鬆重寫
您可以修改代碼以便從文檔加載圖像。接下來的兩個方法從文檔目錄加載所有資產名稱。
-(void) updateData
{
[self.pendingOperations.downloadQueue addOperationWithBlock:^{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *filePathes = [self recursiveRecordsForResourcesOfType:@[@"png", @"jpeg", @"jpg",@"pdf"] inDirectory:documentsDirectory];
@synchronized (self) {
//self.documents = [NSArray arrayWithArray:fileRecords];
self.documents = filePathes;
NSLog(@"documents count %@", @([self.documents count]));
}
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
[self.delegate modelDidUpdate:self];
});
}];
}
- (NSArray *)recursiveRecordsForResourcesOfType:(NSArray *)types inDirectory:(NSString *)directoryPath{
NSMutableArray *filePaths = [[NSMutableArray alloc] init];
NSMutableDictionary *typesDic = [NSMutableDictionary dictionary];
for (NSString *type in types)
[typesDic setObject:type forKey:type];
// Enumerators are recursive
NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:directoryPath];
NSString *filePath;
while ((filePath = [enumerator nextObject]) != nil){
// If we have the right type of file, add it to the list
// Make sure to prepend the directory path
if([typesDic objectForKey:[filePath pathExtension]]){
//[filePaths addObject:[directoryPath stringByAppendingPathComponent:filePath]];
CURFileRecord *record = [CURFileRecord new];
record.filePath =[directoryPath stringByAppendingPathComponent:filePath];
record.fileName = filePath;
[filePaths addObject:record];
}
}
return filePaths;
}
然後創建的NSOperation子類,並把它改寫:
// 3
- (void)main {
// 4
@autoreleasepool {
if (self.isCancelled)
return;
NSData *fileData = [[NSFileManager defaultManager] contentsAtPath:self.fileRecord.filePath];
// self.fileRecord.fileData = fileData;
if (self.isCancelled) {
fileData = nil;
return;
}
if (fileData) {
UIImage *newImage;
if ([[self.fileRecord.filePath pathExtension] isEqualToString:@"pdf"])
{
CGPDFDocumentRef doc = [CURDocumentViewerUtilities MyGetPDFDocumentRef:fileData];
newImage = [CURDocumentViewerUtilities buildThumbnailImage:doc withSize:CGSizeMake(64, 96)];
}
else
{
newImage = [CURDocumentViewerUtilities makePreviewImageFromData:fileData];
}
self.fileRecord.previewImage = newImage;
}
else {
self.fileRecord.failed = YES;
}
fileData = nil;
if (self.isCancelled)
return;
// 5
[(NSObject *)self.delegate performSelectorOnMainThread:@selector(imageDownloaderDidFinish:) withObject:self waitUntilDone:NO];
}
}
希望這有助於
我都用這個,但我的問題不同,因爲我想從設備而不是從載入圖像網絡 –
好的,等一下,我會編輯我的答案:) – Doro