【问题标题】:Download an image to local storage in device IOS7将图像下载到设备IOS7中的本地存储
【发布时间】:2014-07-13 13:23:42
【问题描述】:

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

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

 }

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

谢谢。

【问题讨论】:

    标签: ios imagedownload


    【解决方案1】:

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

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

    然后把这张图片交给image view

       imageview.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/some name", DOCUMENTS_PATH]];
    

    【讨论】:

    • 是的,但您应该在下载完成后分配此图像。
    • 但我希望用户下载图像,如果用户喜欢该图像,他将下载其他图像不需要下载我想要这样我不想自动下载请告诉我该怎么做
    • 如果您使用您编写的其他代码,它也会下载,但它会保存在缓存文件夹中。所以我们最好下载并展示给用户。如果用户不想要,则从文档文件夹中删除
    • 是否可以创建文件夹并保存图片
    • stackoverflow.com/questions/1762836/… 参考此链接了解如何创建文件夹
    猜你喜欢
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多