• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 如何使用 SDWebImage

[复制链接]
菜鸟教程小白 发表于 2022-12-12 21:15:04 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有 UITableViewCell 并在此加载图像,所以我想加载图像并显示激活指示器。这次图像加载时间非常长,所有图像在图像显示在表格 View 后加载。很多时候图像无法加载并且用户 UIInterface 挂起。并且不要在应用程序中处理任何进程。我很累,我使用了 SDWebImage 库。请帮助如何可能,谢谢

#import "UIImageView+WebCache.h"

@interface pictureViewController ()
{
    NSArray*picarray;
    NSArray*titlearray;
    NSArray*idarray;
}

@end

@implementation pictureViewController

- (id)initWithNibNameNSString *)nibNameOrNil bundleNSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[[self navigationController] navigationBar] setTitleTextAttributes{NSForegroundColorAttributeName: [UIColor whiteColor]}];

    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed"background2.png"] forBarMetrics:UIBarMetricsDefault];

    _tableview.separatorColor = [UIColor yellowColor];

    NSURLRequest *req=[[NSURLRequest alloc]initWithURL:[NSURL URLWithString"http://edutimeapp.com/toshow/chamber-of-commerc/ws/fetch_gallery.php"]];
    response =[[NSMutableData alloc]init];
    [NSURLConnection connectionWithRequest:req delegate:self];
}

-(void)connectionNSURLConnection *)connection didReceiveDataNSData *)data
{
    [response appendData:data];
    NSLog(@"error receving data %@",response);
}

-(void)connectionNSURLConnection *)connection didReceiveResponseNSURLResponse *)response
{

}

-(void)connectionDidFinishLoadingNSURLConnection *)connection
{
     NSError *error;

     //NSLog(@"Error in receiving data %@",error);
     NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves  error:&error];
     // NSLog(@"response data %@",json);

      NSArray *results = [json objectForKey"status"];
      picarray = [results valueForKey"img_url"];
      titlearray = [results valueForKey"title"];
      // NSLog(@"my title is%@",titlearray);
      idarray=[results valueForKey"id"];
      //NSLog(@"my galary id is%@",idarray);
      [self.tableview reloadData];
}


// Do any additional setup after loading the view, typically from a nib.
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableViewUITableView *)tableView
{
    return 1;
}

- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section
{
    return [picarray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // NSLog(@"tableview cell");
    picture_viewcontrollerCell *cell = [tableView dequeueReusableCellWithIdentifier"htr"];
    if (cell==nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed"Cell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

     //NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [picarray objectAtIndex:indexPath.row]]];
    // UIImage* image = [[UIImage alloc] initWithData:imageData];
    //cell.picture.image =image;

    cell.titlelbl.text=[NSString stringWithFormat"%@",[titlearray objectAtIndex:indexPath.row]];

    NSURL*url= [[picarray objectAtIndex:indexPath.row]valueForKey:@"img_url"];
    [cell.picture setContentMode:UIViewContentModeScaleAspectFill];
    [cell.picture sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"laceholder.jpg"]];


    return  cell;
}

@end



Best Answer-推荐答案


你应该试试这个(显示activityIndi​​cator & Load Image using SDWebImage)-

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // NSLog(@"tableview cell");
    picture_viewcontrollerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"htr"];
    if (cell==nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"Cell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    cell.titlelbl.text=[NSString stringWithFormat:@"%@",[titlearray objectAtIndex:indexPath.row]];

    NSURL*url= [NSURL URLWithString:[NSString stringWithFormat:@"%@",[picarray objectAtIndex:indexPath.row]]];

    [cell.picture setContentMode:UIViewContentModeScaleAspectFill];

    [cell.picture setShowActivityIndicatorView:YES];

    [cell.picture setIndicatorStyle:UIActivityIndicatorViewStyleGray];

    [cell.picture sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeHolder.png"] options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];

    return  cell;

}

关于ios - 如何使用 SDWebImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38368133/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap