0

我已经尝试找到这个答案好几天了,但找不到任何东西。我将图像的 url 存储在我的数据库中,我试图获取这个 url 并将图像加载到 ImageView。

用户上传一张图片,我的 PHP 脚本为数据库中的图片创建一个 url。这是我的数据库表:ID -> 用户名 -> 密码 -> urlImage。我还有另一个 PHP 脚本,它接受用户名并要求提供 urlImage。

在 Xcode 我想添加这个:` NSUserDefaults *settings = [NSUserDefaults standardUserDefaults]; NSString *user = [设置 valueForKey:@"username"];

NSString *post = [NSString stringWithFormat:@"username=%@&image=dummy",user];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];

[request setURL:[NSURL URLWithString:@"http://url.com/Wish_Profile_Pic.php"]];
NSString *postLen = [[NSString alloc] initWithFormat:@"%d" ,[post length ]];
[request setValue:postLen forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
//this is hard coded based on your suggested values, obviously you'd probably need to make this more dynamic based on your application's specific data to send

[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];

[[NSURLConnection alloc] initWithRequest:request delegate:self];

`

这将向数据库服务器发送请求,服务器将输出 urlImage。如何在 ImageView 中查看图像?找不到任何关于此的内容。

4

1 回答 1

0
NSString *post = [NSString stringWithFormat:@"username=%@&image=dummy",use];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://url.com/Wish_Profile_Pic.php"]];
NSString *postLen = [[NSString alloc] initWithFormat:@"%d" ,[post length ]];
[request setValue:postLen forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *r, NSData *data, NSError *e) {
    if(data.length) {
        UIImage *img = [[[UIImage alloc] initWithData:data] autorelease];

        //try base64 
        if(!img) {
            //NEEDS http://projectswithlove.com/projects/NSData_Base64.zip
           id b64 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
           id data2 = [NSData dataFromBase64String:b64];
           img = [[[UIImage alloc] initWithData:data2] autorelease];
        }
        if(img)
            self.imageView.image = img;
    }
    else if(e)
        NSLog(@"%@", e);
}];
于 2013-05-11T14:25:31.230 回答