gpt4 book ai didi

iOS 图片缓存不正确

转载 作者:行者123 更新时间:2023-11-29 13:06:50 25 4
gpt4 key购买 nike

我在缓存和显示缓存图像时遇到问题。这是一个包含大量图像的 Collection View 。

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
CollectionViewCell *cell = (CollectionViewCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

UIImage *imageMP = [_imageCache objectForKey:[self getImage:indexPath.row]];

NSLog(@"looking for \"%@\"",[self getImage:indexPath.row]);

if (imageMP) {
NSLog(@"found it!");
cell.img_pic.image = imageMP;

}else{
//display loading pic so long
cell.img_pic.image = [UIImage imageNamed:@"photo1.png"];

[self.queue addOperationWithBlock:^{
UIImage *image = [self getActualImage:indexPath.row];
if (image) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
cell.img_pic.image = image;
}];
[_imageCache setObject:image forKey:[self getImage:indexPath.row]];

NSLog(@"saving as \"%@\"",[self getImage:indexPath.row]);
}
}];

}

它正确加载和显示图像,问题在于保存到缓存然后从缓存中显示。

NSLog输出到控制台如下

looking for "/var/mobile/Applications/CAF51B4B-6FAF-4775-A201-EF670BB50462/Documents/7_MP.jpeg"
saving as "/var/mobile/Applications/CAF51B4B-6FAF-4775-A201-EF670BB50462/Documents/7_MP.jpeg"
looking for "/var/mobile/Applications/CAF51B4B-6FAF-4775-A201-EF670BB50462/Documents/7_MP.jpeg"
saving as "/var/mobile/Applications/CAF51B4B-6FAF-4775-A201-EF670BB50462/Documents/7_MP.jpeg"
looking for "/var/mobile/Applications/CAF51B4B-6FAF-4775-A201-EF670BB50462/Documents/7_MP.jpeg"
saving as "/var/mobile/Applications/CAF51B4B-6FAF-4775-A201-EF670BB50462/Documents/7_MP.jpeg"

它永远不会正确显示缓存的图像,否则它会打印“found it!”

我做错了什么?

最佳答案

使用 JMCache ( https://github.com/jakemarsh/JMImageCache ) 然后使用下面的代码

 [cell.img_pic.image setImageWithURL:[NSURL URLWithString:imageUrl] placeholder:[UIImage imageNamed:@"laoding.png"]];

关于iOS 图片缓存不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18440970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com