如何使用SDWebImage

A.偷

我已经UITableViewCell在其中加载图片,因此我要图片加载异步并显示激活指示器。这次图像加载时间非常长,并且所有图像均在表视图中显示图像后加载。很多时候图像无法加载并且用户UIInterface挂起。并且在应用程序中不运行任何进程。.我很累,我使用了一个SDWebImage库。请帮助怎么可能,谢谢

#import "UIImageView+WebCache.h"

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

@end

@implementation pictureViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)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)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [response appendData:data];
    NSLog(@"error receving data %@",response);
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

}

-(void)connectionDidFinishLoading:(NSURLConnection *)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)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)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:@"Placeholder.jpg"]];


    return  cell;
}

@end
罗希特·坎德尔瓦尔(Rohit Khandelwal)

您应该尝试此操作(使用SDWebImage显示activityIndi​​cator和加载图像)-

- (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;

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用SDWebImage库加载图像

来自分类Dev

如何使用SDWebImage下载和保存CGImage

来自分类Dev

如何使用SDWebImage加载webp图像?

来自分类Dev

如何使用SDWebImage向UIButton添加“前景”图像

来自分类Dev

如何使用SDWebImage在iOS中设置渐进式图像?

来自分类Dev

如何在SDwebimage中使用成功和失败块

来自分类Dev

如何使用带有UICollectionView的SDWebImage添加分解动画

来自分类Dev

如何在SDwebimage中使用成功和失败块

来自分类Dev

如何使用SDWebImage将图像从JSON加载到UIImageView中?

来自分类Dev

如何使用SDWebImage在cgcontext中绘制图像

来自分类Dev

如何使用SDWebImage来缓存多个尺寸的图像

来自分类Dev

SDWebImage 如何配置 SDImageCache

来自分类Dev

使用SDWebImage的setImage

来自分类Dev

无法使用SDWebImage

来自分类Dev

使用SDWebImage的setImage

来自分类Dev

对SDWebImage使用单例

来自分类Dev

如何从SDWebImage获取图像大小?

来自分类Dev

如何从SDWebImage获取图像大小?

来自分类Dev

使用SDWebImage忽略查询参数

来自分类Dev

在Swift中使用SDWebImage的问题

来自分类Dev

使用SDWebimage的UIButton中的setBackgroundimage

来自分类Dev

使用Swift解析SDWebImage实现

来自分类Dev

使用 FirebaseStorageUI + SDWebImage 时出错

来自分类Dev

如何将 alpha 属性添加到使用 SDWebImage 设置的 UIImage 中?

来自分类Dev

SDWebImage 4 - 如何取消下载请求?

来自分类Dev

使用SDWebImage时表视图滚动滞后

来自分类Dev

使用SDWebImage处理非图片URL

来自分类Dev

UICollection使用SDWebImage查看滚动滞后

来自分类Dev

使用SDWebImage时表视图滚动滞后