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