ios - 如何在uitableviewcell中设置来自服务器的图像路径

标签 ios uitableview uiimageview

我正在制作一个应用程序,其中我从服务器获取数据并且数据图像路径也即将到来但是当我将图像设置到我的 tableview 单元格应用程序将变得太重可能 b 我没有正确设置图像下面是我的示例代码 thanx 提前 :)

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *tableviewidentifier = @"cell";
    tablecellTableViewCell *cell= [self.activitiesTableView_ dequeueReusableCellWithIdentifier:tableviewidentifier];

    if(cell==nil)
    {
        cell = [[tablecellTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:tableviewidentifier];
    }if(indexPath.row == [self tableView:tableView numberOfRowsInSection:indexPath.section] - 1){
        // [[cell textLabel] setText:@"Load more records"];
    }

    UILabel *valuedate = (UILabel *)[cell viewWithTag:21];
    UILabel *msg = (UILabel *)[cell viewWithTag:22];
    UILabel *date = (UILabel *)[cell viewWithTag:23];
    UILabel *time = (UILabel *)[cell viewWithTag:24];
    valuedate.text=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"offerTitle"];
    msg.text=@"How are you?";
    NSString *img=[[self.inboxmessagesarray objectAtIndex:indexPath.row]objectForKey:@"offerPhoto"];// here i am getting image path
     UIImage *img1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:img]]];
    cell.imageView.image=img1;// here i am setting image due to which app is so heavy and stuck


    return cell;
}

最佳答案

不要使用 imageWithData: 来设置图像。它是同步的,会使您的应用运行缓慢。

而不是使用 SDWebImage

你只需要做以下事情:

  1. 将 SDWebImage 文件夹转储到您的项目中。

  2. 导入UIImageView+WebCache.h

  3. 使用以下方法设置图像:sd_setImageWithURL:

来自GCD (Grand Central Dispatch) and sending asynchronous requests .代码复制自 HERE .

首先实现下面的方法。

- (void)downloadImageWithURL:(NSURL *)url completionBlock:(void (^)(BOOL succeeded, UIImage *image))completionBlock
{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                               if ( !error )
                               {
                                    UIImage *image = [[UIImage alloc] initWithData:data];
                                    completionBlock(YES,image);
                                } else{
                                    completionBlock(NO,nil);
                                }
                           }];
}

然后在您的cellForRowAtIndexPath

[self downloadImageWithURL:your_url completionBlock:^(BOOL succeeded, UIImage *image) {
            if (succeeded) {
                // change the image in the cell
                cell.imageView.image = image;


            }
        }];

关于ios - 如何在uitableviewcell中设置来自服务器的图像路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27399235/

相关文章:

iphone - ios uiscrollview 与 uiimageviews 之间启用分页如何检测哪个在屏幕上/可见

ios - Firebase 和 SwiftUI 实时预览的问题

iphone - 更改 UIView 动画转换背后的背景

iphone - UIView的UIImagePickerController

swift - 重排序时如何保持细胞颜色特征?

ios - UIImageView 的内存大小

ios - 根据设备的大小更改 UIImageViews 的位置

iphone - 为什么 self.navigationController.visibleViewController 在不可见的 View Controller 上返回 true

swift - (Swift) 在 ViewController 的容器 View 中显示 TableViewController

arrays - 错误 SIGABRT - 无法在我的 TableViewController 中加载多个数组