0

when the purchase is completed in my app, i am downloading the images from server to the application.

I am able to download a image from online and i write inside iPhone using asihttprequest.

i have checked inside the iphone's simulator documents directory , the downloaded file is exist.

how i am doing is , i have placed a uibutton inside the tableview when clicking the button, it starts downloading and displays it inside the uitableviewcell.

up-to this it is fine.when going back and coming to the same uitableview, the image is not displayed.it simply empty.when again i tap uibutton it starts downloading from online.

i am not sure how to display the locally saved image? and how to display image which is downloaded and written into the app and if image is not availabale inside the app , then need to start downloading from the server.

Please guide me

Please is my code

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath 
//-------------------------------------------------------------------------------
{
    int tablePadding = 40;
    int tableWidth = [self.tblPoemTypes frame].size.width;
    if (tableWidth > 480) { 
        tablePadding = 110;
    }


    static NSString *CellIdentifier = @"PoemTypeCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero]autorelease];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;



            // Only load cached images; defer new downloads until scrolling ends
           /* if (!psTypeTags.image)
            {
                cellImageView.image = [UIImage imageNamed:@"no-image-available.jpg"]; 
                [cell.contentView addSubview:cellImageView];

                [self startIconDownload:psTypeTags forIndexPath:indexPath];
            }
            else
            {
                NSLog(@"YES Image *****************");
                cellImageView.image = psTypeTags.image;
                [cell.contentView addSubview:cellImageView];
            }*/

            //----added by re

            if([indexPath row]==0)
            {

                goButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                [goButton setTitle:@"Go" forState:UIControlStateNormal];
                [goButton sizeToFit];
                [goButton setFrame:CGRectMake(220,30,50,30)];

                [goButton addTarget:self action:@selector(fetchThreeImages1:) forControlEvents:UIControlEventTouchUpInside];
                [cell addSubview:goButton];

                imageView1 = [[[UIImageView alloc] initWithFrame:CGRectZero] autorelease];
                [imageView1 setBackgroundColor:[UIColor grayColor]];


                [imageView1 setImage:[array objectAtIndex:0]];
                [cell addSubview:imageView1];

                imageProgressIndicator1 = [[[UIProgressView alloc] initWithFrame:CGRectZero] autorelease];
                [cell addSubview:imageProgressIndicator1];  

            }

        NSUInteger imageWidth = (tableWidth-tablePadding-20)/3;
        NSUInteger imageHeight = 35;

        [imageView1 setFrame:CGRectMake(tablePadding/2,20,imageWidth,imageHeight)];

        [imageProgressIndicator1 setFrame:CGRectMake(120,40,imageWidth,20)];


            //-----------------

              }

    return cell;
}
- (void)fetchThreeImages1:(id)sender
{
    [imageView1 setImage:nil];

    if (!networkQueue) {
        networkQueue = [[ASINetworkQueue alloc] init];  
    }
    failed = NO;
    [networkQueue reset];
    [networkQueue setRequestDidFinishSelector:@selector(requestForDownloadOfFileFinished:)];
    [networkQueue setRequestDidFailSelector:@selector(requestForDownloadOfFileFailed:)];



    [networkQueue setShowAccurateProgress:YES];
    [networkQueue setDelegate:self];
    self.request=nil;

    NSURL *url;

    NSString *urlString=@"http://cdn.visual-blast.com/wp-content/uploads/2012/03/1700-royalty-free-stock-images-48x48.jpg";
    url = [NSURL URLWithString:urlString];
    request = [ASIHTTPRequest requestWithURL:url];


    [request setDownloadProgressDelegate:imageProgressIndicator1];
    [request setUserInfo:[NSDictionary dictionaryWithObject:@"request1" forKey:@"name"]];
    [request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"1.png"]];
    [request setShouldContinueWhenAppEntersBackground:YES];
    [request setDelegate:self];
    [request setDidReceiveDataSelector:@selector(request:didReceiveBytes:)]; 
    [request setShowAccurateProgress:YES];

    [networkQueue addOperation:request];


    [networkQueue go];

}
- (void)requestForDownloadOfFileFinished:(ASIHTTPRequest *)request1
{

    NSLog(@"req finish.........");
    NSLog(@"Content will be %llu bytes in size",[request1 contentLength]); 

    goButton.hidden=YES;
    [imageProgressIndicator1 removeFromSuperview];
    UIImage *img = [UIImage imageWithContentsOfFile:[request1 downloadDestinationPath]];
    array=[[[NSMutableArray alloc]init]autorelease];
    [array addObject:img];
    image = [[UIImage alloc ]initWithData:[request1 responseData]];


    NSString *receivedString = [request1 responseString];
    NSLog(@"received string %@",receivedString);
    NSData *responseData = [request1 responseData];

    NSString *response = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    NSLog(@"Server response:%@", response);

    NSLog(@"response data %@",[request1 responseData]);
    NSLog(@"download destination path %@",[request downloadDestinationPath]);
    NSLog(@"download destination path1 %@",[request1 downloadDestinationPath]);
    NSLog(@"image %@",img);
    NSLog(@"image1 %@",image);
    if (img) {
        [imageView1 setImage:img];
    }
    UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Download" message:@"Download Completed" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
    [alertView show];
    //completed=true;
    NSLog(@"mutablearray count %@",[array objectAtIndex:0]);

    //[self.tblPoemTypes reloadData];

}

- (void)requestForDownloadOfFileFailed:(ASIHTTPRequest *)request1
{
    if (!failed) {
        if ([[request1 error] domain] != NetworkRequestErrorDomain || [[request1 error] code] != ASIRequestCancelledErrorType) {
            UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Download failed" message:@"Failed to download images" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
            [alertView show];
        }
        failed = YES;

    }

}
4

2 回答 2

1

After the image is downloaded you have to save the image say in document directory.So when you launch the tableview first check whether that image is present in the document, if it is present directly display the image as:

Edit:

 - (NSString *)applicationDocumentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

-(void)loadimage{
    NSString *workSpacePath=[[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"your image-name"];
    UIImageView *myimage=[UIImageView alloc] initWithFrame:CGRectMake(0,0,20,20)];
    myimage.image=[UIImage imageWithData:[NSData dataWithContentsOfFile:workSpacePath]];    
    [self.view addSubView:myimage];
    [myimage release];
}

If it not present then download it.

于 2012-07-03T13:08:05.330 回答
0

have a look at this project: https://github.com/rs/SDWebImage/

于 2012-07-03T13:09:04.133 回答