0

I make a Application that contain tableview with image and it Contain JSON Data.it is working Good But image downloading take upto 15minutes how do i fast? My code for image Cache is

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

     }];

it Shows CashedImage Perfectly but take a time to Downloaded image that can not be cashed please Give me Solution For that.

Ashish Gabani
  • 445
  • 1
  • 5
  • 30
  • Please do not add whole code. add only that code which is related to your question and issue. Maybe your internet connection is low and your image size is big. Please check this link and change `maxConcurrentDownloads` to 1 or 2 whatever your want (http://stackoverflow.com/questions/11396425/sdwebimage-is-there-a-limit-on-how-many-setimagewithurl-can-be-queued) – ChintaN -Maddy- Ramani Oct 29 '14 at 12:50
  • Where i add this Code on my application #Chinttu? – Ashish Gabani Oct 29 '14 at 12:54
  • in `appDidFinishLaunching` and `SDWebImageManager.sharedManager.imageDownloader.executionOrder = SDWebImageDownloaderLIFOExecutionOrder;` for download image in LIFO order. so when you scroll table last image would be download first. – ChintaN -Maddy- Ramani Oct 29 '14 at 12:56
  • if this answer helps you then i'll update it as an answer and you can close the question. – ChintaN -Maddy- Ramani Oct 30 '14 at 04:15
  • Yes Bro it was Working.But in my this Code when i scroll Table view It Parse Second Page Data And Load All second Page data But i want only Show 4 cell at a time how it possible? – Ashish Gabani Oct 30 '14 at 04:33

0 Answers0