简体   繁体   English

从网上安全地为UIImageView加载图像

[英]Load image for UIImageView from the net with security

I'm trying to load an image into an UIImageView from the internet. 我正在尝试从互联网将图像加载到UIImageView中。 This is no problem, but what I want is the image be secured with user credentials. 没问题,但是我想要的是使用用户凭据保护图像。 So if you go to the direct link of the image, you get an login popup (secured directory). 因此,如果您转到图像的直接链接,则会看到一个登录弹出窗口(安全目录)。

How can I pass those user credentials so I can load the image from the net with user credentials? 如何传递这些用户凭据,以便可以使用用户凭据从网络加载图像?

The problem is that the image may not be accessed directly, but only via the iPhone app. 问题在于图像可能无法直接访问,而只能通过iPhone应用程序访问。 If you have a better idea to maximize the security, let me know ;-) 如果您有更好的想法来最大化安全性,请告诉我;-)

Thanks in advance. 提前致谢。

If you are using basic server authentication (which I think you are based on the "popup dialog"), I would recommend using ASIHTTPRequest http://allseeing-i.com/ASIHTTPRequest/How-to-use 如果您使用的是基本服务器身份验证(我认为您基于“弹出对话框”),则建议您使用ASIHTTPRequest http://allseeing-i.com/ASIHTTPRequest/How-to-use

It makes it very easy to download and make web requests. 它使下载和发出Web请求变得非常容易。 You can download the image and give the username and password by doing: 您可以执行以下操作下载图像并提供用户名和密码:

NSURL *url = [NSURL URLWithString:@"http://user:passwd@yoursite.com/secret/"];
ASIHTTPRequest *req = [ASIHTTPRequest requestWithURL:url];
[req startSynchronous];
NSError *err = [req error];
if (!err) {
    NSData *response = [req responseData];  
    UIImage* image = [UIImage imageWithData:response];
    // do whatever you want with the image here, like put it into a UIImageView
}

I'm assuming you have access to the server side. 我假设您有权访问服务器端。 Create a simple PHP script that takes in an HTTP POST request with the username and password, validates it and if it's valid, echoes back a JSON/XML/string with the link to the image or else echoes back a null value or something. 创建一个简单的PHP脚本,该脚本使用用户名和密码接收HTTP POST请求,对其进行验证(如果有效),并通过指向图像的链接回显JSON / XML / string,或者回显null值或其他内容。

Call a POST request from your iphone using urlWithString and when you get a response, check to see if it's null, or if it has a link. 使用urlWithString从您的iPhone调用POST请求,当您收到响应时,检查它是否为null或是否具有链接。

Handle the result accordingly (if null, tell the user that the login credentials are incorrect). 相应地处理结果(如果为null,则告诉用户登录凭据不正确)。

If not null, it's the image - display it. 如果不为null,则为图片-显示它。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM