如何在 UICollectionView 中更快地加载图像?
How to load images faster in UICollectionView?
我有一组图像,其中包含图像的完整路径。
我将图像存储在文档目录中。
问题是当我向下滚动时,collection 视图中的图像加载时间很慢,因此滚动也很差。
Referencing 此代码使其更快。
但问题是图像没有显示在单元格中。
我的 collection 查看代码是
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
AlbumImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AlbumImageCell" forIndexPath:indexPath];
cell.albumImage.image=nil;
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"AlbumImageCell" owner:self options:nil] objectAtIndex:0];
}
NSString *path=[documentImage objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:path];
// note that this can be a web url or file url
ImageRequest *request = [[ImageRequest alloc] initWithURL:url];
UIImage *image = [request cachedResult];
if (image) {
UIImageView *imageView = (UIImageView *)[cell viewWithTag:127];
imageView.image = image;
cell.albumImage.image=image;
} else {
[request startWithCompletion:^(UIImage *image, NSError *error) {
if (image && [[collectionView indexPathsForVisibleItems] containsObject:indexPath]) {
[self.album_ImageView reloadItemsAtIndexPaths:@[indexPath]];
}
}];
}
return cell;
}
我在 UIImage 中得到了 null。
请帮我解决这个问题。
好的,你可以用这个,
如果你想在 UICollectionView 中快速加载图片,请使用 'EGOImageView',EGOImageView 下载图片速度快,滚动灵活,请参考以下 link 下载 EGOImageView Loader,
下载 ZIP 文件,然后从您的代码中集成以下 classes,
- EGOCahce
- EGOImageLoader
- EGOImageButton
- EGOImageView
如果集成所有 Class,将 class 导入 UICollectionView Class,
#import "EGOImageView/EGOImageView.h"
#import "EGOImageLoader/EGOImageLoader.h"
如果导入class,则声明下面给出的 EGOImageView 示例,
@property (retain, nonatomic) EGOImageView *stickerImageView;
在下面的代码中使用'-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath'获取url并粘贴代码,
NSURL *url = [NSURL URLWithString:path];
if (url)
[cell.stickerImageView setImageURL:url];
else
[cell.stickerImageView setImage:@"placeholderImage"];
它对我有用...
我强烈推荐使用此库在 collection 视图单元格或 table 视图中使用 NSURL
加载图像:
据我所知,它是最常用的异步图像下载器之一,可能使用了一些内部缓存机制。您需要根据要显示图像的 UI 元素使用 import different category headers 。在这种情况下,您需要包括
#import <SDWebImage/UIImageView+WebCache.h>
请找到我在下面显示的示例代码,我用它来为 collection 视图单元格中的每个单元格设置图像视图。
NSURL *imageURL = [NSURL URLWithString:@"http://www.you-url-goes-here.png"];
[_sampleImageView sd_setImageWithURL:imageURL placeholderImage:[UIImage defaultPlaceHolderImage] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (image == nil) {
UIImage *localImage = [UIImage someLocalImage];
if (localImage == nil) {
localImage = [UIImage defaultPlaceHolderImage];
}
_sampleImageView.image = localImage;
}
}];
Buddy just go with the AFNetworking library.It 很容易使用,你的图片会很容易获取,它会自动缓存,你的 collection 不会 stuck.You 会得到滚动的高性能
从这里下载 https://github.com/AFNetworking/AFNetworking
UIImageView+AFNetworking.h
使用以上类别获取图片
NSString *path=[documentImage objectAtIndex:indexPath.row];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:path isDirectory:NO];
UIImageView *sample=[[UIImageView alloc] init];
[sample sd_setImageWithURL:fileURL placeholderImage:[UIImage imageNamed:@"PHOTO.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (image == nil) {
UIImage *localImage = [UIImage imageNamed:@"PHOTO.png"];
if (localImage == nil) {
localImage = [UIImage imageNamed:@"PHOTO.png"];
}
sample.image = localImage;
}
cell.albumImage.image=image;
}];
我有一组图像,其中包含图像的完整路径。 我将图像存储在文档目录中。 问题是当我向下滚动时,collection 视图中的图像加载时间很慢,因此滚动也很差。 Referencing 此代码使其更快。
但问题是图像没有显示在单元格中。 我的 collection 查看代码是
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
AlbumImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AlbumImageCell" forIndexPath:indexPath];
cell.albumImage.image=nil;
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
if (cell == nil) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"AlbumImageCell" owner:self options:nil] objectAtIndex:0];
}
NSString *path=[documentImage objectAtIndex:indexPath.row];
NSURL *url = [NSURL URLWithString:path];
// note that this can be a web url or file url
ImageRequest *request = [[ImageRequest alloc] initWithURL:url];
UIImage *image = [request cachedResult];
if (image) {
UIImageView *imageView = (UIImageView *)[cell viewWithTag:127];
imageView.image = image;
cell.albumImage.image=image;
} else {
[request startWithCompletion:^(UIImage *image, NSError *error) {
if (image && [[collectionView indexPathsForVisibleItems] containsObject:indexPath]) {
[self.album_ImageView reloadItemsAtIndexPaths:@[indexPath]];
}
}];
}
return cell;
}
我在 UIImage 中得到了 null。 请帮我解决这个问题。
好的,你可以用这个,
如果你想在 UICollectionView 中快速加载图片,请使用 'EGOImageView',EGOImageView 下载图片速度快,滚动灵活,请参考以下 link 下载 EGOImageView Loader,
下载 ZIP 文件,然后从您的代码中集成以下 classes,
- EGOCahce
- EGOImageLoader
- EGOImageButton
- EGOImageView
如果集成所有 Class,将 class 导入 UICollectionView Class,
#import "EGOImageView/EGOImageView.h"
#import "EGOImageLoader/EGOImageLoader.h"
如果导入class,则声明下面给出的 EGOImageView 示例,
@property (retain, nonatomic) EGOImageView *stickerImageView;
在下面的代码中使用'-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath'获取url并粘贴代码,
NSURL *url = [NSURL URLWithString:path];
if (url)
[cell.stickerImageView setImageURL:url];
else
[cell.stickerImageView setImage:@"placeholderImage"];
它对我有用...
我强烈推荐使用此库在 collection 视图单元格或 table 视图中使用 NSURL
加载图像:
据我所知,它是最常用的异步图像下载器之一,可能使用了一些内部缓存机制。您需要根据要显示图像的 UI 元素使用 import different category headers 。在这种情况下,您需要包括
#import <SDWebImage/UIImageView+WebCache.h>
请找到我在下面显示的示例代码,我用它来为 collection 视图单元格中的每个单元格设置图像视图。
NSURL *imageURL = [NSURL URLWithString:@"http://www.you-url-goes-here.png"];
[_sampleImageView sd_setImageWithURL:imageURL placeholderImage:[UIImage defaultPlaceHolderImage] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (image == nil) {
UIImage *localImage = [UIImage someLocalImage];
if (localImage == nil) {
localImage = [UIImage defaultPlaceHolderImage];
}
_sampleImageView.image = localImage;
}
}];
Buddy just go with the AFNetworking library.It 很容易使用,你的图片会很容易获取,它会自动缓存,你的 collection 不会 stuck.You 会得到滚动的高性能 从这里下载 https://github.com/AFNetworking/AFNetworking
UIImageView+AFNetworking.h
使用以上类别获取图片
NSString *path=[documentImage objectAtIndex:indexPath.row];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:path isDirectory:NO];
UIImageView *sample=[[UIImageView alloc] init];
[sample sd_setImageWithURL:fileURL placeholderImage:[UIImage imageNamed:@"PHOTO.png"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if (image == nil) {
UIImage *localImage = [UIImage imageNamed:@"PHOTO.png"];
if (localImage == nil) {
localImage = [UIImage imageNamed:@"PHOTO.png"];
}
sample.image = localImage;
}
cell.albumImage.image=image;
}];