如何从使用SDWebImage库的Json解析中下载更快更快的图像?

时间:2014-10-29 12:39:05

标签: ios json uicollectionview sdwebimage

我创建了一个包含tableview with image的应用程序,它包含JSON Data.it工作正常但图像下载需要15分钟,我如何快速? 我的图像缓存代码是

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0)
- (void)viewDidLoad
{
[self.spinner startAnimating];
[super viewDidLoad];
Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];

if (networkStatus == NotReachable)
{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"No Internet Avaliable" message:@"Please Check YOur Internet Connection" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];
}
else
{

pageNum=0;
self.imageArray =[[NSMutableArray alloc]init];
NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.truemanindiamagazine.com/webservice/news.php?page=%d",pageNum]];
[self.newsTable setShowsHorizontalScrollIndicator:NO];
[self.newsTable setShowsVerticalScrollIndicator:NO];
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
    data = [NSData dataWithContentsOfURL: url];
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}

}
-(void)fetchedData:(NSData *)responsedata
{
if (responsedata.length >0)
{
    NSError* error;
    self.json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
    if ([[_json objectForKey:@"data"] isKindOfClass:[NSArray class]])
    {
        NSArray *arr = (NSArray *)[_json objectForKey:@"data"];
        [self.imageArray addObjectsFromArray:arr];
        [self.newsTable reloadData];
        NSLog(@"images,%@",self.imageArray);
    }
    if (self.imageArray.count == 0)
    {
        self.newsTable.scrollEnabled=NO;
    }
    else
    {
        self.newsTable.scrollEnabled=YES;
    }

  }
  [self.spinner stopAnimating];
   self.spinner.hidesWhenStopped=YES;
 }
 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {
 return self.imageArray.count;
  }
 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
if (self.imageArray == nil || self.imageArray.count <1)
{
    return 0;
}
else
{
    return 1;
  }
  }
 -(CustumCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *Cellidentifier=@"Cell";
CustumCell *cell=[tableView dequeueReusableCellWithIdentifier:Cellidentifier];
if (cell ==nil)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustumCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}
{
    [cell.spinner startAnimating];
    NSDictionary *dict = [self.imageArray objectAtIndex:indexPath.section];
    NSString *img2=[dict valueForKey:@"post_image"];

    [cell.imagePhoto sd_setImageWithURL:[NSURL URLWithString:[img2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[UIImage imageNamed:@"Setting.png"] options:SDWebImageHighPriority completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
     {
            [cell.spinner stopAnimating];
             cell.spinner.hidesWhenStopped=YES;

     }];

它显示CashedImage完美但是花时间来下载无法兑现的图像 请给我解决方案。

0 个答案:

没有答案