मैं उद्देश्य-सी के लिए काफी नया हूं इसलिए उम्मीद है कि यह सब समझ में आता है। मैंने सर्वर से छवियां डाउनलोड की हैं और उन्हें collectionView cellForItemAtIndexPath:
विधि में छवि दृश्य में प्रदर्शित किया है। जिस मुद्दे का सामना कर रहा हूं वह यह है कि छवियां कैशिंग नहीं लगती हैं। ऐसा लगता है कि हर बार एक सेल का पुन: उपयोग किया जा रहा है, सर्वर से संबंधित छवि फिर से डाउनलोड की जा रही है। प्रलेखन पढ़ने और इसी तरह के सवाल मैंने सोचा था कि इस के जवाब को देख सेएक छवि कैशिंग और UICollectionView
imageDictionary = [[NSMutableDictionary alloc]initWithCapacity:50.0];
के साथ निम्न कोड पर्याप्त होगा:
मेरी viewDidLoad विधि में मैं एक NSMutableDictionary बना रहा हूं। मैं इस पर कुछ दिनों से रहा हूं और मुझे पता है कि कुछ ऐसा है जो मुझे याद आ रहा है या एक अवधारणा जिसे मैं समझ नहीं रहा हूं।
#pragma mark - UICollectionView Data Source
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;{
NSLog(@"Begin retrieving photos");
return [self.photos count];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;{
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MY_CELL" forIndexPath:indexPath];
cell.imageView.image = nil;
if (cell.imageView.image == nil) {
dispatch_queue_t downloadQueue = dispatch_queue_create("image downloader", NULL);
dispatch_async(downloadQueue, ^{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[self.photos objectAtIndex:indexPath.row] objectForKey:@"fullimage"]]];
UIImage *image = [UIImage imageWithData:data];
[imageDictionary setObject:image forKey:@"Image"];
dispatch_async(dispatch_get_main_queue(), ^{
cell.imageView.image = [imageDictionary objectForKey:@"Image"];
[cell setNeedsDisplay];
});
});
}
return cell;
}
किसी भी मदद की सराहना की जाएगी। अग्रिम में धन्यवाद।
ऐसा लगता है कि आप हर बार छवि को फिर से लोड कर रहे हैं। संग्रहदृश्य आपके लिए छवियों को कैश नहीं करता है। आपको कैशिंग के लिए अपना खुद का शब्दकोश बनाना होगा। एनएससीएच में देखो। – yuf
@ यूफ करेंगे - धन्यवाद! – BrianS