2013-05-11 85 views
0

我试图找到这个答案几天,现在找不到任何东西。我存储url在我的数据库中的图像,我试图得到这个URL并加载图像到ImageView。将MYSQL加载到Imageview的图像

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

在Xcode中,我想补充一句:' NSUserDefaults * settings = [NSUserDefaults standardUserDefaults]; NSString * user = [设置valueForKey:@“用户名”];

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中查看图像?无法找到任何关于此。

回答

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); 
}]; 
+0

我会尝试这个权利了,但看起来像是我需要:) – user2351814 2013-05-11 14:27:36

+0

这样做后异步,当它回来,你从响应数据 – 2013-05-11 14:28:01

+0

获取图像我在viewDidLoad中尝试这样做,不得不将此行更改为self> imageView.image = img;但没有看到我的imageview中的任何东西。你有一个教程或什么?或者你是怎么跟这个去的?它应该只是简单的做一个IBOutlet UIImageView * imageView在.h和你的代码在.m的权利? – user2351814 2013-05-11 14:45:34