下载并将图像保存到根目录 [英] download and save image to root

查看:79
本文介绍了下载并将图像保存到根目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何下载图像并将其保存到应用程序的根目录,因此基本上我可以通过

How would i download and save an image to the root of the application so basically i can access the image via

[UIImage imageNamed:@"myimage.jpg"];

谢谢

梅森

推荐答案

首先,您需要获取图像数据

First you need to get the Image Data

NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://media03.linkedin.com/mpr/mpr/shrink_80_80/p/3/000/064/2e2/1bd3849.jpg"]];

然后您需要将数据写入Documents目录

Then you need to write the data to Documents directory

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *pathLD = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"imageLD%d.jpeg",[[NSDate date] timeIntervalSince1970]]]; 

NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: pathLD]){
    [imageData writeToFile:pathLD atomically:YES];
} else {
    NSLog(@"File exists at path:%@", pathLD);
}

要从文档"中获取图像,请执行以下操作:

To get the image from Documents you do:

[UIImage imageWithContentsOfFile:pathLD];

祝你好运:D;

这篇关于下载并将图像保存到根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆