繁体   English   中英

使用customCell在tableView中加载三个图像

[英]Loading three images in tableView with customCell

我创建了一个带有customCelltableView ,它的每一行都有三张图像。我已经从文件夹中加载了图像。 当我向上或向下滚动时, tableView会花一些时间向上或向下移动。 我发现了一些概念,例如缓存和延迟加载,它们是从服务器加载图像的。 我不知道如何在我的应用程序中使用它。

ImagesClass *Obj1 = [imageLists objectAtIndex:indexPath.row*noOfImageInRow];


    UIImage *image1 = [self getImageForImageId:Obj1.imageId FromPath:SAVEDIMAGE_DIR];
    Obj1.thumbImage = [self imageWithImage:image1 convertToSize:CGSizeMake(130, 130)];

    [cell setImage:1 :Obj1.thumbImage RowNo:indexPath.row*noOfImageInRow];


}

if ([imageLists count] > indexPath.row*noOfImageInRow+1) {

    ImagesClass *Obj2 = [imageLists objectAtIndex:indexPath.row*noOfImageInRow+1];
    UIImage *image1 = [self getImageForImageId:Obj2.imageId FromPath:SAVEDIMAGE_DIR];
    Obj2.thumbImage = [self imageWithImage:image1 convertToSize:CGSizeMake(130, 130)];

    [cell setImage:2 :Obj2.thumbImage RowNo:indexPath.row*noOfImageInRow+1];


}

if ([imageLists count] > indexPath.row*noOfImageInRow+2) {


    ImagesClass *Obj3 = [imageLists objectAtIndex:indexPath.row*noOfImageInRow+2];

    UIImage *image1 = [self getImageForImageId:Obj3.imageId FromPath:SAVEDIMAGE_DIR];
    Obj3.thumbImage = [self imageWithImage:image1 convertToSize:CGSizeMake(130, 130)];

    [cell setImage:3 :Obj3.thumbImage RowNo:indexPath.row*noOfImageInRow+2];


}

cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.delegate = self;
return cell;

我建议您使用此AsyncImageView 调用此API:

ASyncImage *img_EventImag = alloc with frame;
NSURL *url = yourPhotoPath;
[img_EventImage loadImageFromURL:photoPath];
[self.view addSubView:img_EventImage]; // In your case you'll add in your TableViewCell.

与使用UIImageView相同。 简单易行,它可以为您完成大部分工作。 AsyncImageView包括UIImageView上的一个简单类别(用于在iOS上异步加载和显示图像,以使它们不会锁定UI)以及UIImageView子类(用于更高级的功能)。 AsyncImageView使用URL,因此可以与本地或远程文件一起使用。

加载/下载的图像会缓存在内存中,并在出现内存警告时自动清除。 AsyncImageView的运行独立于UIImage缓存,但是默认情况下,位于应用程序捆绑包根目录中的所有图像都将存储在UIImage缓存中,从而避免了缓存图像的任何重复。

该库还可以用于独立于UIImageView加载和缓存图像,因为它提供对基础加载和缓存类的直接访问。

试试这个简单的方法

如果您在本地加载图像,请尝试以下代码

在ViewDidLoad下执行此操作

 Image1Array=[[NSMutableArray alloc]initwithobjects:[UIImage imagenamed@"cell1stimage.png"],....nil];
 Image2Array=[[NSMutableArray alloc]initwithobjects:[UIImage imagenamed@"cell2ndimage.png"],....nil];
 Image3Array=[[NSMutableArray alloc]initwithobjects:[UIImage imagenamed@"cell3rdimage.png"],....nil];

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [Image1Array count];
}

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];




    UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 42, 42)];
    [imageview setImage:[Image1Array objectAtIndex:indexpath.row];
    [cell.contentView addSubview:imageview];
    [imageview release];


    UIImageView *imageview2=[[UIImageView alloc]initWithFrame:CGRectMake(85, 0, 42, 42)];
    [imageview setImage:[Image2Array objectAtIndex:indexpath.row];
    [cell.contentView addSubview:imageview];
    [imageview2 release];


    UIImageView *imageview3=[[UIImageView alloc]initWithFrame:CGRectMake(0, 120, 42, 42)];
    [imageview3 setImage:[Image3Array objectAtIndex:indexpath.row];
    [cell.contentView addSubview:imageview3];
    [imageview3 release];
    }

    return cell;
    }

希望这可以帮助..!

如果发现图像仍然加载缓慢,则在数组中添加UIImageview而不是UIimage

有时以前我已经做到了。 我的经验是,无论是从文件夹加载还是从网络加载,都必须将缩略图副本保存在本地。 这样加载起来会更快。

对于每张图像,我都将缩略图副本保存在/ thumbnail文件夹中。 如果对于某些图像没有缩略图,则存储一张,然后加载它们。 通过这种方式,表格单元可以更快地加载图像。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM