【发布时间】:2016-03-10 08:02:45
【问题描述】:
当我创建用户(模型)对象时,我正在使用 SDWebImage(在我的模型中)异步加载 UITableViewCells 中的用户图像。下载图像时,我在 tableviewcell(View) 中显示 NSActivityIndicator 代替图像。当图像被下载时,我重新加载我的表格视图。因为我将这些 NSActivityIndicators 放在了我的单元格中,所以我没有其他方法可以阻止它们,只能通过通知(在 SDWEbimage 方法完成处理程序内)。 所以问题是根据 MVC 架构,这是否是正确的方法?
这是我的模型类(员工)的 sn-p:
self.employeeID = [self stringValueForKey:@"id"];
self.name = [self stringValueForKey:@"name"];
self.company = [self stringValueForKey:@"company"];
self.companyID = [self numberValueForKey:@"companyId"];
self.photo = [UIImage imageNamed:@"photoPlaceholder"];
// fetch Emploee photo
NSString *photoURL = [NSString stringWithFormat:@"http://blabla.com//img/photo/%@.png",self.employeeID];
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:photoURL]
options:0
progress:^(NSInteger receivedSize, NSInteger expectedSize)
{
} completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished)
{
if (image && finished)
{
self.photo = image;
[[NSNotificationCenter defaultCenter] postNotificationName:@"IMAGE_FINISHED_DOWNLOADING" object:self];
// make updates to database from Main Thread to avoid race conditions
dispatch_async(dispatch_get_main_queue(), ^{
NSFetchRequest *request = [NSFetchRequest new];
[request setEntity:[NSEntityDescription entityForName:@"EmployeeCD" inManagedObjectContext:[AppDelegate getDelegate].managedObjectContext]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"employeeID == %@",self.employeeID];
[request setPredicate:predicate];
NSError *err;
NSArray *arr = [[AppDelegate getDelegate].managedObjectContext executeFetchRequest:request error:&err];
if ([arr count]) {
((EmployeeCD*)arr.lastObject).photo = UIImagePNGRepresentation(image);
[[AppDelegate getDelegate] saveContext];
}
});
}
else
{
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
UIImage *img = [UIImage imageFromColor:[UIColor groupTableViewBackgroundColor]];
UIImage *scaledImage = [img imageByScalingAndCroppingForSize:CGSizeMake(200, 200)];
UIImage *roundImage = [UIImage roundImage:scaledImage imageView:imgView];
self.photo = roundImage;
[[NSNotificationCenter defaultCenter] postNotificationName:@"IMAGE_COULDNT_DOWNLOAD" object:self];
}
}];
}
【问题讨论】:
标签: ios objective-c uitableview model-view-controller sdwebimage