2013-05-11 98 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