我有一个与 Parse 相关的应用程序,我在使用自定义单元格显示表格视图时出现问题UIImageView.
首次尝试下载照片时, 用户界面停止响应约1-2秒。
问题似乎是UIImage *image = [UIImage imageWithData: data];
,它不在主要队列中,但仍然阻止 UI 。
有什么问题?
我使用的代码是:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
MyEvent *currentEvent = self.tableViewElements[indexPath.section][indexPath.row]; //class that stores event details without UIImages. UIImages are stored in a NSMutableDictionary
NSDate *currentDate = currentEvent.dateStart;
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:currentDate];
InvitationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InvitationTableViewCell"];
cell.eventImageView.tag = indexPath.row;
cell.eventImageView.image = nil;
cell.eventNameLabel.text = currentEvent.name;
cell.eventDayLabel.text = [NSString stringWithFormat:@"%d", [components day]];
cell.eventMonthLabel.text = monthsNameArray[[components month] -1];
if ([[AppModel sharedModel].myEventsImagesDictionary objectForKey:currentEvent.eventId])
{
UIImage *image = [[AppModel sharedModel].myEventsImagesDictionary objectForKey:currentEvent.eventId];
cell.eventImageView.image = image;
}
else
if (currentEvent.pictureFile)
{
dispatch_queue_t downloadQueue = dispatch_queue_create("Image Downloader", NULL);
dispatch_async(downloadQueue, ^{
//currentEvent.pictureFile is a PFFile
[currentEvent.pictureFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
if (cell.eventImageView.tag == indexPath.row)
{
UIImage *image = [UIImage imageWithData: data];
UIImage *croppedImage = [Utility cropImage:image toFitInSize:CGSizeMake(320.0, 90.0)];
image = nil;
[[AppModel sharedModel].myEventsImagesDictionary setObject:croppedImage forKey: currentEvent.eventId];
dispatch_async(dispatch_get_main_queue(), ^{
if (croppedImage)
{
cell.eventImageView.image = croppedImage;
}
});
}
}
}];
});
}
return cell;
}