脱机模式下的ios存储URL图像(未连接到internet)
我正在从不同的URL获取图像数组。图像正在加载。但是如果我需要在脱机模式下显示图像。我找不到正确的方法来获取。这是我的代码脱机模式下的ios存储URL图像(未连接到internet),ios,xcode,uiimage,uibutton,nsurlconnection,Ios,Xcode,Uiimage,Uibutton,Nsurlconnection,我正在从不同的URL获取图像数组。图像正在加载。但是如果我需要在脱机模式下显示图像。我找不到正确的方法来获取。这是我的代码 #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *arr = [NSArray arrayWithObjects:[NSURL URLWithString:
@"http://images.wikia.com/marvelmovies/images/5/5e/The-hulk-2003.jpg"],
[NSURL URLWithString:@"http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],
[NSURL URLWithString:@"http://challenge.roadef.org/2012/img/google_logo.jpg"],
[NSURL URLWithString:@"http://addyosmani.com/blog/wp-content/uploads/2012/03/Google-doodle-of-Richard-007.jpg"],
[NSURL URLWithString:@"http://techcitement.com/admin/wp-content/uploads/2012/06/apple-vs-google_2.jpg"],
[NSURL URLWithString:@"http://www.andymangels.com/images/IronMan_9_wallpaper.jpg"],
[NSURL URLWithString:@"http://sequelnews.com/wp-content/uploads/2011/11/iphone_5.jpg"],Nil];
NSURL *url=[NSURL URLWithString:arr];
NSData* theData = [NSData dataWithContentsOfURL:url];
int row = 0;
int column = 0;
for(int i = 0; i < [(NSArray*) url count]; ++i) {
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:(NSURL*) [(NSArray*)url objectAtIndex:i]]];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(column*100+16, row*90, 80, 80);
[button setImage:image forState:UIControlStateNormal];
[button addTarget:self
action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[self.view addSubview:button];
if (column == 2) {
column = 0;
row++;
} else {
column++;
}
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:arr];
[theData writeToFile:localFilePath atomically:YES];
}
#导入“ViewController.h”
@界面视图控制器()
@结束
@实现视图控制器
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
NSString*arr=[NSArray arrayWithObjects:[NSURL URLWithString:
@"http://images.wikia.com/marvelmovies/images/5/5e/The-hulk-2003.jpg"],
[NSURL URLWithString:@”http://cdn.gottabemobile.com/wp-content/uploads/2012/06/ios62.jpg"],
[NSURL URLWithString:@”http://challenge.roadef.org/2012/img/google_logo.jpg"],
[NSURL URLWithString:@”http://addyosmani.com/blog/wp-content/uploads/2012/03/Google-doodle-of-Richard-007.jpg"],
[NSURL URLWithString:@”http://techcitement.com/admin/wp-content/uploads/2012/06/apple-vs-google_2.jpg"],
[NSURL URLWithString:@”http://www.andymangels.com/images/IronMan_9_wallpaper.jpg"],
[NSURL URLWithString:@”http://sequelnews.com/wp-content/uploads/2011/11/iphone_5.jpg“],无];
NSURL*url=[NSURL URLWithString:arr];
NSData*数据=[NSData DATA with contents sofURL:url];
int行=0;
int列=0;
对于(int i=0;i<[(NSArray*)url计数];++i){
UIImage*image=[UIImage imageWithData:[NSData dataWithContentsOfURL:(NSURL*)[(NSArray*)url对象索引:i]];
UIButton*button=[UIButton button类型:UIButtonTypeCustom];
button.frame=CGRectMake(列*100+16,行*90,80,80);
[按钮设置图像:图像状态:uicontrol状态正常];
[按钮添加目标:自我
操作:@选择器(按钮点击:)
forControlEvents:UIControlEventTouchUpInside];
button.tag=i;
[self.view addSubview:按钮];
如果(列==2){
列=0;
行++;
}否则{
列++;
}
}
NSArray*Path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString*documentsDirectory=[paths objectAtIndex:0];
NSString*localFilePath=[DocumentsDirectoryStringByAppendingPathComponent:arr];
[数据写入文件:本地文件路径原子:是];
}
指南请…尝试,
SDWebImage
可以下载图像并保存到磁盘以供缓存,您可以在脱机时使用。尝试,SDWebImage
可以下载图像并保存到磁盘以供缓存,您可以在脱机时使用。您应该将图像保存到您的设备上,因此,您可以在未连接到internet时访问它们。你现在这样做的方式总是加载url
您可以这样尝试:
对每个图像使用NSData对象并加载其中的url内容
NSData* theData = [NSData dataWithContentsOfURL:yourURLHere];
现在,您可以将此对象保存到设备中,并在需要时访问它
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:@"picName.jpg"];
[theData writeToFile:localFilePath atomically:YES];
应该就是这样了您应该将图像保存到您的设备中,这样您就可以在未连接到internet时访问它们。你现在这样做的方式总是加载url 您可以这样尝试: 对每个图像使用NSData对象并加载其中的url内容
NSData* theData = [NSData dataWithContentsOfURL:yourURLHere];
现在,您可以将此对象保存到设备中,并在需要时访问它
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:@"picName.jpg"];
[theData writeToFile:localFilePath atomically:YES];
这应该就是问题所在了@Dany大声呼喊,去掉结尾的逗号就可以了……我会编辑这个问题,只是问谷歌会给你这个链接@Dany大声呼喊,只要去掉结尾的逗号就可以了……我会编辑这个问题,只要问一下谷歌会给你这个链接,你就可以把这些图片缓存在缓存文件夹中,这样你就不需要一次又一次地下载这些图片,当你处理完这些图片后,就可以从缓存中删除这些图片。你可以在缓存中缓存这些图片缓存文件夹,这样您就不需要一次又一次地下载这些图像,当您处理这些图像的工作完成后,就可以从缓存中删除这些图像。这不是一个好方法(第一部分),因为对于可能需要太多时间的进程来说,它不是异步的。这不是一个好方法(第一部分),因为对于可能需要花费太多时间的进程来说,它不是异步的。