0

Hi in my application I have image which change every day as per the date now i want to give download option for the user like they have to download by clicking button and they can see in there device. I'm searching for long time to achieve this task but not able get the proper solution please tell me how to achieve this one.

My ImageView code.

- (void)viewDidLoad
 {
    [super viewDidLoad];
     //this is url which gives the date has a echo 
     NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"url"]];

    [request setHTTPMethod:@"GET"];
    [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];
     NSError *err;

     NSURLResponse *response;

     NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

     NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];

    //This is for Response

   // here I'm connecting the date to my url do display the image 

    NSString  *imgstr=[NSString stringWithFormat:@"url",resSrt];

    //assign your image view name
    imageview.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",imgstr] ]]];

 }

I have used the above to do display the image now to want to user download the image by clicking the download button to localstorage of the device please tell me how to achieve this.

Thanks.

Shanthanu
  • 633
  • 1
  • 9
  • 30

1 Answers1

1

First download the image synchronously and save it in documents folder, then show it over image view.

    NSURL *url = [NSURL URLWithString: @"url"];
    NSString *imagePath = [DOCUMENTS_PATH stringByAppendingPathComponent:@"some name"];
    NSError *error = nil;
    NSData *imageData = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error: &error];
    if (!error) {
        [imageData writeToFile:imagePath atomically:YES];

    }

Then give this image to the image view

   imageview.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/some name", DOCUMENTS_PATH]];
Ganee....
  • 262
  • 2
  • 13
  • yes, but you should assign this image once download is completed. – Ganee.... May 24 '14 at 06:22
  • but i want user to download the image if user likes the image he will download othersiwse no need to download i want like that i dont want to download automatically please tell me how to do that – Shanthanu May 24 '14 at 06:24
  • If you use other code like you wrote, it will also download but it will save in the cache folder. So better we download and showing to the user. If user don't want that then delete from the documents folder – Ganee.... May 24 '14 at 06:36
  • is there any possiablilities of create folder and save the image – Shanthanu May 24 '14 at 06:46
  • http://stackoverflow.com/questions/1762836/create-a-folder-inside-documents-folder-in-ios-apps refer this link to know how to create folder – Ganee.... May 24 '14 at 06:48