0

嗨,在我的应用程序中,我的图像每天都会根据日期更改,现在我想为用户提供下载选项,就像他们必须通过单击按钮下载一样,他们可以在设备中看到。我正在寻找很长时间来完成这项任务,但无法获得正确的解决方案,请告诉我如何实现这一目标。

我的 ImageView 代码。

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

 }

我现在已经使用上面的方法来显示图像,希望用户通过单击下载按钮将图像下载到设备的本地存储,请告诉我如何实现这一点。

谢谢。

4

1 回答 1

1

首先同步下载图片并保存在文档文件夹中,然后在图片视图中显示。

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

    }

然后将此图像提供给图像视图

   imageview.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/some name", DOCUMENTS_PATH]];
于 2014-05-24T06:19:35.430 回答