我有一个包含三列“缩略图”、“文件名”和“日期修改”的 NSTableview。文件名列和修改日期列由 S3ListObjectResponse 的输出填充。而“缩略图”列图像是从机器的本地目录加载的。如果文件丢失,我会从 S3 云下载。问题是如何将数据插入到 NSArrayController 如果文件丢失并且我留下了一个空单元格作为缩略图?这是我现在的工作:
- (IBAction)checkCloud:(id)sender {
AmazonS3Client *s3 = [AmazonClientManager s3];
S3ListObjectsRequest* listObjectsRequest = [[S3ListObjectsRequest alloc] initWithName:@"hello-testing"];
NSRange range = NSMakeRange(0, [[_fileListAC arrangedObjects] count]);
[_fileListAC removeObjectsAtArrangedObjectIndexes:[NSIndexSet indexSetWithIndexesInRange:range]];
@try {
S3ListObjectsResponse* response = [s3 listObjects:listObjectsRequest];
NSMutableArray* objectSummaries = response.listObjectsResult.objectSummaries;
//looping through the objSummary and add into the NSArrayController
for ( S3ObjectSummary* objSummary in objectSummaries ) {
NSImage *thumbnail = [[NSImage alloc] init ];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *s3date = [dateFormatter dateFromString:[objSummary lastModified]];
NSArray *fileName = [[objSummary key] componentsSeparatedByString:@"/"];
if([[fileName objectAtIndex:0] localizedCaseInsensitiveCompare:@"Range"] == NSOrderedSame) {
NSLog(@"file name %@ ",[fileName objectAtIndex:1]);
// Check for files in side the bucket's folders
if([[fileName objectAtIndex:1] length]){
NSString *thumbnailFile = [[fileName objectAtIndex:1] stringByAppendingString:@"_thumb.png"];
BOOL isDir;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePathPart1 = [@"/Users/" stringByAppendingString:[[NSHost currentHost] localizedName]];
NSString *filePath = [[[[ filePathPart1 stringByAppendingString:@"/Documents/Test/" ] stringByAppendingString:@"Range" ] stringByAppendingString:@"/Thumbnail/"] stringByAppendingString:thumbnailFile];
// If file exists then add into the thumbnail column
if([fileManager fileExistsAtPath:filePath isDirectory:&isDir]){
thumbnail = [[NSImage alloc] initWithContentsOfFile:filePath];
}else{
NSLog(@"file %@ not found",thumbnailFile);
//Downloading from the S3 Cloud Asynchronously
doneDownload = NO;
AmazonS3Client *s3 = [AmazonClientManager s3];
S3GetObjectRequest *gor = nil;
@try {
gor = [[S3GetObjectRequest alloc] initWithKey:[[@"Range/Thumbnail/" stringByAppendingString:[fileName objectAtIndex:1]] stringByAppendingString: @"_thumb"] withBucket:@"thumbnail-pic" ];
gor.delegate = self;
[s3 getObject:gor];
}
@catch (AmazonClientException *exception) {
doneDownload = YES;
}
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
} while (!doneDownload);
gor.delegate = nil;
}
}else{
//If its the actual bucket folder and not file then load a default image
thumbnail = [NSImage imageNamed:@"Range.png"];
}
}
[_fileListAC addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:thumbnail,@"thumbnail", [objSummary key],@"Files",s3Date,@"Date Modified", nil]];
}
}
@catch (NSException *exception) {
NSLog(@"Cannot list S3 %@",exception);
}
//Display the table on loading the NSArrayController
[_cloudFileList makeKeyAndOrderFront:nil];
}
我检查了委托方法
-(void)request:(AmazonServiceRequest *)request didCompleteWithResponse:(AmazonServiceResponse *)response{
NSData *imageData = [response body];
_coreDataImageView.image = [[NSImage alloc] initWithData:imageData];
doneDownload = YES;
}
coredataimageview 是一个测试图像。图像已下载,但我想将其添加回阵列控制器。问题是,如果本地目录中缺少 3 个文件,并且我无法将该文件名和日期修改值插入到数组控制器中,该怎么办。对此有什么帮助吗?