Ios 将MYSQL的图像加载到Imageview
我已经试着找到这个答案好几天了,但什么也找不到。我将图像的url存储在数据库中,并尝试获取此url并将图像加载到ImageView 用户上传一个图像,我的PHP脚本在数据库中创建一个指向该图像的url。这是我的数据库表:ID->Username->Password->urimage。我还有另一个PHP脚本,它接受用户名并询问urlImage 在Xcode中,我想添加以下内容:` NSUserDefaults*设置=[NSUserDefaults standardUserDefaults]; NSString*user=[设置值forkey:@“username”] `Ios 将MYSQL的图像加载到Imageview,ios,objective-c,image,post,loading,Ios,Objective C,Image,Post,Loading,我已经试着找到这个答案好几天了,但什么也找不到。我将图像的url存储在数据库中,并尝试获取此url并将图像加载到ImageView 用户上传一个图像,我的PHP脚本在数据库中创建一个指向该图像的url。这是我的数据库表:ID->Username->Password->urimage。我还有另一个PHP脚本,它接受用户名并询问urlImage 在Xcode中,我想添加以下内容:` NSUserDefaults*设置=[NSUserDefaults standardUserDefaults]; NS
这将向数据库服务器发送请求,服务器将输出urlImage。如何在ImageView中查看图像?我找不到关于这个的任何东西 我会立即尝试,但看起来像我需要的:)这会进行后期异步,当它返回时,您会从响应数据中获取图像我在ViewDidLoad中尝试了这一点,并且不得不将这一行更改为self->imageView.image=img;但在我看来什么都看不到。你有这方面的教程吗?或者你是怎么做到的?在.h中创建一个IBMOutlet UIImageView*imageView,在.m中创建代码应该很简单,对吧?是的。如果您有imageView,只需设置其图像。设置url时,请确保url正常,返回的数据正常,并且图像不为零。在行上放置一个断点。image=imgalso检查您返回的数据是否是base64编码的,它可能是:)然后您需要对其进行解码。。。我将用这个更新示例
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);
}];
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);
}];