【发布时间】:2013-08-30 17:31:26
【问题描述】:
超级编辑:
好的,经过几次不同的修改,我有一个更明确的问题。基本上我正在努力使用 NSCache。我有一个集合视图,如下所示,我希望能够最大程度地减少图像的滚动延迟,我大部分时间都在这样做。困难的部分是移动到全尺寸图像。它在另一个视图中,我希望过渡到全尺寸照片,就像 Apple 使用他们的照片应用程序所做的那样。
似乎 Apple 正在缓存图像并以不同的方式访问它们。我发现我需要加载原始缩略图,然后在下载全尺寸图像时我可以替换它。但我不知道该怎么做。
到目前为止,我拥有的是:
UICollection 视图
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.myCache = [[NSCache alloc] init];
}
//Reusable cell structure
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//cell
CoverPhotoCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"CoverImgCell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor blackColor];
cell.clipsToBounds = YES;
cell.opaque = YES;
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
Media *object = [[Media alloc] init];
object = [self.photoListArray objectAtIndex:indexPath.item];
//Remove old cell
for (UIView *subview in [cell subviews]){
if (subview.tag != 0 )
{
[subview removeFromSuperview];
}
}
//UIImage
UIImage *thumbImg = [_myCache objectForKey:object.url];
if (thumbImg) {
cell.imageView.image = thumbImg;
}
else {
cell.imageView.image = nil;
UIImage *thumbImg = [[UIImage alloc] initWithCGImage:[object.asset aspectRatioThumbnail]];
dispatch_async(dispatch_get_main_queue(), ^{
cell.imageView.image = thumbImg;
});
[_myCache setObject:thumbImg forKey:object.url];
}
//thumbnail
cell.imageView.tag = indexPath.row + 1;
cell.imageView.layer.borderWidth = 5;
cell.imageView.layer.borderColor = [UIColor whiteColor].CGColor;
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
cell.imageView.frame = CGRectMake(0, 0, 125, 125);
return cell;
}
//User taps image
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"PhotoSegue"]) {
DetailPageController *controller = segue.destinationViewController;
NSIndexPath *selectedIndex = [self.collectionView indexPathsForSelectedItems][0];
controller.initialIndex = (NSUInteger)selectedIndex.item;
controller.photos = self.photoListArray;
}
}
这就是我的 NSCache 代码,然后我使用 segue 到显示全分辨率照片的全视图控制器。如何先加载缩略图,然后再加载全尺寸图像。
还有关于苹果如何如此快速地加载图像的想法吗?他们是否使用不同的缓存方式?还是我做错了什么。
精致编辑:
有人可以发布或至少将我指向一些示例代码,当用户快速滚动时我会显示缩略图并在他们放慢速度时加载实际图像,类似于我猜苹果电影预告片应用程序。
【问题讨论】:
-
“困难的部分是移动到全尺寸图像。它在另一个视图中,我希望过渡到全尺寸照片,就像 Apple 使用他们的照片应用程序所做的那样” i> - 你有一个特定的指标吗? “似乎Apple正在缓存图像并以不同方式访问它们。我发现我需要加载原始缩略图,然后在下载全尺寸图像时我可以替换它。但我不确定如何做到这一点。” 你能澄清一下吗?他们如何“以不同的方式”访问它?具体是什么你不确定?
标签: ios objective-c uicollectionview