gpt4 book ai didi

ios - 快速在 TableView 中加载图像线程

转载 作者:行者123 更新时间:2023-11-30 14:16:38 25 4
gpt4 key购买 nike

在我正在制作的应用程序中,我需要能够在 tableView(_:) 函数创建单元格后将图像加载到表格单元格中。我发现这可以通过线程来完成,但我不太确定它是如何完成的。

有人知道如何在 tableView(_:) 加载单元格的其余部分后启动一个单独的线程,以便将图像添加到单元格中,同时保持在单元格中的平滑滚动表格 View 。

最佳答案

这是您研究的良好起点

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];

}
}

希望这有帮助

关于ios - 快速在 TableView 中加载图像线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31086093/

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