在我的 iOS 应用程序中,我需要从 Web 加载图像,但该图像位于需要提供用户名和密码的服务器上。我尝试使用此代码:
- (void) loadImageFromWeb:(NSString *)urlImg {
NSURL* url = [NSURL URLWithString:urlImg];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse * response,
NSData * data,
NSError * error) {
if (!error){
UIImage* image = [[UIImage alloc] initWithData:data];
[self.imageObjectScanned setImage:image];
} else {
NSLog(@"ERRORE: %@", error);
}
}];
}
但它返回一个错误并说:
Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x14e6b420 {NSErrorFailingURLKey=http://54.204.6.246/magento8/media/catalog/product/cache/0/image/9df78eab33525d08d6e5fb8d27136e95/s/c/scheda_non_shop.jpg, NSErrorFailingURLStringKey=http://54.204.6.246/magento8/media/catalog/product/cache/0/image/9df78eab33525d08d6e5fb8d27136e95/s/c/scheda_non_shop.jpg, NSUnderlyingError=0x14e695c0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1012.)"}
我猜这个错误是因为我没有发送用户名和密码。如何发送用户名和密码来加载此图像?