我几天都试图找到这个答案而且找不到任何东西。我将url存储到我的数据库中的图像,我试图获取此URL并将图像加载到ImageView。
用户上传图像,我的PHP脚本为数据库中的图像创建了一个URL。这是我的数据库表:ID - >用户名 - >密码 - > urlImage。我有另一个PHP脚本,它接受用户名和urlImage的问题。
在Xcode中我想添加:` NSUserDefaults * settings = [NSUserDefaults standardUserDefaults]; NSString * user = [settings 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中查看图像?无法找到任何相关信息。
答案 0 :(得分: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);
}];