我正在制作带有图片时间轴的应用,到目前为止,我已完成以下操作:
问题在于步骤#3。它可以工作,但它同时加载所有这些,目前时间轴中的48个图片正在吃掉537Mb的内存。 idk怎样做才能让它变得流畅,占用更少的资源,如Instagram和Facebook。
我在uitableviewcontroller中显示图片。有没有人对如何制定时间表有任何想法?
要获取图片:
if(feedArray.count > 0){
for(NSString *person in feedArray){
int count = [[Globals global] getMiscPicsCountForUser:person];
for(int i = 1; i < count; i++){
[[Globals global] getMiscPicForUser:person :i withFetchedPicture:^(WKImage *image) {
if(image){
[self.thisPersonsTimelineObjects addObject:image];
//[self createTimelineViewForImage:image];
NSLog(@"person: %@ -- count:%i -- temp array count:%lu -- i:%i", person, count, self.thisPersonsTimelineObjects.count ,i);
if(self.thisPersonsTimelineObjects.count == count - 1){
[self sortTimelineByDate];
}
}
}];
}
}
}
-(void)createTimelineViewForImage :(WKImage *)image {
dispatch_async(dispatch_queue_create("timeline", NULL), ^{
dispatch_async(dispatch_get_main_queue(), ^{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:[[NSBundle mainBundle].infoDictionary objectForKey:@"UIMainStoryboardFile"] bundle:[NSBundle mainBundle]];
WKTimelineViewsViewController *view = [storyboard instantiateViewControllerWithIdentifier:@"WKTimelineViewViewController"];
[view initWithImage:image andParentController:self];
view.view.frame = CGRectMake(view.view.frame.origin.x,
view.view.frame.origin.y,
view.view.frame.size.width * 0.95,
view.view.frame.size.height * 0.95);
[self.thisPersonsTimelineViews addObject:view];
[self.tableView reloadData];
});
});
}
-(void)sortTimelineByDate {
if(self.thisPersonsTimelineObjects.count > 0){
NSMutableArray *dateArray = [NSMutableArray array];
for(WKImage *view in self.thisPersonsTimelineObjects){
[dateArray addObject:[view timeStamp]];
}
NSMutableArray *tempArray = [NSMutableArray array];
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO];
NSArray *descriptors = [NSArray arrayWithObject:descriptor];
NSArray *reverseOrder = [dateArray sortedArrayUsingDescriptors:descriptors];
for(int i = 0; i < reverseOrder.count; i++){
NSDate *date = reverseOrder[i];
for(int j = 0; j < self.thisPersonsTimelineObjects.count; j++){
NSDate *timestamp = [self.thisPersonsTimelineObjects[j] timeStamp];
if(date == timestamp){
[tempArray addObject:self.thisPersonsTimelineObjects[j]];
}
}
}
self.thisPersonsTimelineObjects = [NSMutableArray arrayWithArray:tempArray];
[self.thisPersonsTimelineViews removeAllObjects];
for(WKImage *image in self.thisPersonsTimelineObjects){
[self createTimelineViewForImage:image];
}
}
}
答案 0 :(得分:0)
您遇到了问题,因为所有图片都是预先提取的。相反,你想要做的是在用户滚动时获取图像,然后在下载时缓存它们(这是Instagram和Facebook所做的)。
首先,我建议将SDWebImage集成到您的项目中,因为这样可以更轻松。
接下来,您可以集成以下逻辑:
以下是一个例子:
1和2(在数据源中):
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
PhotoObject *object = [self objects][[indexPath row]];
[[cell imageView] sd_setImageWithURL:[object photoURL]];
return cell;
}
3 :(在表格单元格内)
-(void)prepareForReuse {
[super prepareForReuse];
[[self imageView] sd_cancelCurrentImageLoad];
[[self imageView] setImage:nil];
}
这将允许图像仅按原样下载,并且您还将使用SDWebImage自动缓存它们,以便在用户滚动回图像时立即显示而不是下载。