【发布时间】:2015-01-20 05:43:16
【问题描述】:
您好,我正在使用 iOS 中的 url 从服务器获取单个图像。
我的代码是这样的
- (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];
//我的Array在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 从服务器获取多张图像。请任何人 提前谢谢你
【问题讨论】:
-
没有给你问题
-
非常感谢您回复我..如何使用 url 从服务器下载多个图像..我从包含一个图像的 url 获取单个图像..但没有从包含多个图像的 url 获取多个图像图片
-
你可以把png名字存成0v1.png.ov2.png....然后把所有的url字符串都拉出来
-
你的意思是所有的 png 图像都保存到一个 url 中?还是所有 png 图片 url 都保存到一个通用 url 中?
-
实际上我的问题是从 url 下载图像,即显示
标签: ios nsurlconnection nsurlrequest nsmutableurlrequest