如何在iOS中使用url从服务器获取多个图像
嗨,我正在使用iOS中的url从服务器获取单个图像 我的代码是这样的如何在iOS中使用url从服务器获取多个图像,ios,nsurlconnection,nsurlrequest,nsmutableurlrequest,Ios,Nsurlconnection,Nsurlrequest,Nsmutableurlrequest,嗨,我正在使用iOS中的url从服务器获取单个图像 我的代码是这样的 - (IBAction)overlaysClicked:(id)sender { NSLog(@"overlays Clicked"); request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://sukhada.co.in/img/overlays/neon/ov1.png"]]; [NSURLConnect
- (IBAction)overlaysClicked:(id)sender
{
NSLog(@"overlays Clicked");
request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://sukhada.co.in/img/overlays/neon/ov1.png"]];
[NSURLConnection connectionWithRequest:request delegate:self];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:@"image.jpg"];
NSData *thedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://sukhada.co.in/img/overlays/neon/ov1.png"]];
[thedata writeToFile:localFilePath atomically:YES];
UIImage *img = [[UIImage alloc] initWithData:thedata];
self.overlayImgView.image=img;
}
要从服务器获取多个图像,请使用下面的代码
NSURL *myUrl = [NSURL URLWithString:@"http://sukhada.co.in/img/overlays/neon.zip"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
myData = [[NSMutableData alloc] initWithLength:0];
NSURLConnection *myConnection = [[NSURLConnection alloc] initWithRequest:myRequest delegate:self startImmediately:YES];
//我的数组在viewDidload中是这样的
self.overlaysImgsArray = [[NSMutableArray alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://sukhada.co.in/img/overlays/neon.zip"]];
NSLog(@"urls is %@",overlaysImgsArray);
for (int i=0; i<[overlaysImgsArray count]; i++)
//download array have url links
{
NSURL *URL = [NSURL URLWithString:[overlaysImgsArray objectAtIndex:i]];
NSMutableURLRequest *urlRequest = [[NSMutableURLRequest alloc]initWithURL:URL];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if([data length] > 0 && [[NSString stringWithFormat:@"%@",error] isEqualToString:@"(null)"])
{
//make your image here from data.
UIImage *imag = [[UIImage alloc] initWithData:[NSData dataWithData:data]];
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [array objectAtIndex:0];
NSString *imgstr=[NSString stringWithFormat:@"%d",i];
NSString *pngfilepath = [NSString stringWithFormat:@"%@sample%@.png",docDir,imgstr];
NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(imag)];
[data1 writeToFile:pngfilepath atomically:YES];
}
else if ([data length] == 0 && [[NSString stringWithFormat:@"%@",error] isEqualToString:@"(null)"])
{
NSLog(@"No Data!");
}
else if (![[NSString stringWithFormat:@"%@",error] isEqualToString:@"(null)"]){
NSLog(@"Error = %@", error);
}
}];
}
}
但这对我不起作用,请任何人建议我如何使用一个包含所有图像的url从服务器获取多个图像。请任何人
提前谢谢您您可以这样试试 首先,您需要保存目录中的所有10个图像,并根据需要逐个获取。此代码保存url中的所有图像 试试这个
- (IBAction)overlaysClicked:(id)sender {
//Note all your image saved with ov1.png,ov2.png......& so .
for (int i=1; i<=10; i++) {
NSString *st2=@"ov";
NSString *st1=@"http://sukhada.co.in/img/overlays/neon/ov";
NSString *imageN=[st2 stringByAppendingString:[NSString stringWithFormat:@"%d",i]];
NSString *imgNameforkey=[imageN stringByAppendingString:@".png"];
NSString *url=[st1 stringByAppendingString:[NSString stringWithFormat:@"%d",i]];
NSString *imgname=[url stringByAppendingString:@".png"];
NSLog(@" all=%@",imgname);
NSLog(@"overlays Clicked");
request = [NSURLRequest requestWithURL:[NSURL URLWithString:imgname]];
[NSURLConnection connectionWithRequest:request delegate:self];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:imgname];
NSData *thedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgname]];
[thedata writeToFile:localFilePath atomically:YES];
UIImage *img = [[UIImage alloc] initWithData:thedata];
NSLog(@"imgs %@",img);
// self.overlayImgView.image=img;
}
}
- (IBAction)overlaysClicked:(id)sender {
dispatch_queue_t queue = dispatch_get_global_queue(0,0);
dispatch_async(queue, ^{
NSLog(@"Beginning download");
NSString *stringURL = @"http://sukhada.co.in/img/overlays/neonra.zip";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
//Find a cache directory. You could consider using documenets dir instead (depends on the data you are fetching)
NSLog(@"Got the data!");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
//Save the data
NSLog(@"Saving");
NSString *dataPath = [path stringByAppendingPathComponent:@"img.zip"];
dataPath = [dataPath stringByStandardizingPath];
[urlData writeToFile:dataPath atomically:YES];
});
}
从url下载zip文件并保存。在按钮操作中使用此代码。现在不需要循环。试试这个
- (IBAction)overlaysClicked:(id)sender {
dispatch_queue_t queue = dispatch_get_global_queue(0,0);
dispatch_async(queue, ^{
NSLog(@"Beginning download");
NSString *stringURL = @"http://sukhada.co.in/img/overlays/neonra.zip";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
//Find a cache directory. You could consider using documenets dir instead (depends on the data you are fetching)
NSLog(@"Got the data!");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
//Save the data
NSLog(@"Saving");
NSString *dataPath = [path stringByAppendingPathComponent:@"img.zip"];
dataPath = [dataPath stringByStandardizingPath];
[urlData writeToFile:dataPath atomically:YES];
});
}
并检查您将获得img.zip文件
没有收到您的回复非常感谢您的回复..如何使用url从服务器下载多个图像..我从包含一个图像的url获得单个图像..但是没有从包含多个图像的url获得多个图像您可以保存png名称,如0v1.png.ov2.png….然后将其全部拉入url字符串是常见的您的意思是全部吗png图像是否保存到一个url?或者所有png图像url都保存到一个公共url?实际上我的问题是从url下载图像,即显示请告诉我哪里出错了。?它显示了imgs nulthankq-soo对我的建议。我也有这个疑问,但当我们点击该url时,它正在下载并在下载文件夹中显示10幅图像。这个过程是否正确?否则,当我们点击该url时,我们想打开新标签并显示所有10幅图像。是吗?让我们来看看。我面临另一个问题。我有NSLog中的书面文档目录路径..每次在循环中它都显示相同的路径..当我们检查该路径时,它只显示ov1.png图像,但URL不同