loading image fro MYSQL into Imageview - ios

I have tried to find this answer for days now and cant find anything. I store url to image in my database and Im trying to get this url and load the image to a ImageView.
Users uploads a image and my PHP script makes a url to the image in the database. This is my database table: ID -> Username -> Password -> urlImage. And I have another PHP script that takes the username and askes for the urlImage.
In Xcode i want to add this: `
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];
`
This will send a request to the database sever and the server will output the urlImage. How can I view the image in the ImageView? Cant find anything about this.

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

Related

Issue with IOS Http POST

Request data
NSDictionary *tmp = #{#"name":#"Kousik",#"age":#"24",#"location":#"bangalore"};
NSString *postdata = [NSString stringWithFormat:#"request = %#",tmp];
//now postdata is
//request = {
"age" = "24";
"location" = "bangalore";
"name" = "Kousik";
}
but I want this NSDictionary should be inside a string so that in server I can eval it and get the dictionary back.
Here I want something like toString() is java or str() in pyhton.
This is my Http request:-
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:path]];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
NSError *error;
NSData *preparedPostData = [postdata dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postdata length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:preparedPostData ];
[NSURLConnection sendAsynchronousRequest:request
queue:backgroundQueue
completionHandler:^(NSURLResponse response,NSData data,NSError *error){
NSString *result = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
if(complect)
complect(result,error);
}
];
Update
My request is going to server properly but The problem is with data structure.
when I am trying to access the data with request key then it is giving error because the value for the request key is not supported in server as this value is having = sign in between. So what I want is that to make the requset data structure properly.want to send the NSDictionary itself as a string.
postdata should be something like this
request = "{
age : 24;
location : bangalore;
name : Kousik;
}"
I dont want to use application/json.
NSString *stringURL = [NSString stringWithFormat:#"name=%#&age=%#&location=%#",#"Kousik",#"24",#"bangalore"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#%#",ServerURL,API_SaveContctListToAddressBook]]];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[stringURL dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
// NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (!data) {
data = [[NSData alloc] init];
}
NSDictionary *dic =[ NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&connectionError];
completionHandler(dic, NO);
}];
Use NSJSONSerialization to convert your dictionary to NSData and then attach it on HTTPBody
NSData *httpBody = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#%#",ServerURL,APIPath]]];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:httpBody];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//Handling code
}];

How do I convert this curl command to Objective-C

the command:
curl -X POST -F "search={\"page\":\"1\",\"county\":\"18\",\"type\":\"J\"} " -H "Host: 118932.ro" http://118932.ro/dezv/ipa/index.php
This is what I have so far:
url = [NSURL URLWithString:#"http://www.118932.ro/dezv/ipa/index.php"];
NSData *data = [NSJSONSerialization dataWithJSONObject:self.requestData options:0 error:&error];
NSString *bodyString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *dataString = [NSString stringWithFormat:#"\"search=%#\"",bodyString];
NSData *dataBody = [dataString dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:dataBody];
[request setValue:#"118932.ro" forHTTPHeaderField:#"Host"];
where self.requestData is the following dictionary
self.requestData = #{#"page" : #"1", #"county" : #"18", #"type" : #"J"};
but this doesnt seem to give me any results after I do this
self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[self.connection start];
Here is the code I use. The -F parameter in cUrl specifies the actual post data, so you may want to use the string "search={\"page\":\"1\",\"county\":\"18\",\"type\":\"J\"} " as is. You should call this code in a background thread as the synchronous call will block the UI.
/**
* Do an HTTP POST request and return the results
*
* #param NSString * url the URL
* #param NSString * post the POST data
*
* #return NSDictionary * a dictionary containing the response, error, and data
*/
+ (NSDictionary *)httpPostRequestWithUrl:(NSString *)url post:(NSString *)post
{
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:#"%d", (int)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
[request setTimeoutInterval:30]; // set timeout for 30 seconds
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSMutableDictionary *rc = [[NSMutableDictionary alloc] init];
if ( response )
[rc setObject:response forKey:#"response"];
if ( error )
[rc setObject:error forKey:#"error"];
if ( data )
[rc setObject:data forKey:#"data"];
return (NSDictionary *)rc;
}

How to use GData framework in iOS app to upload images to Picasa?

In my app, I have an imageView, I want to post the image to Picasa.
I got some suggestion to use GData Framwork. I downloaded it from
code.google.com/p/gdata-objectivec-client/downloads/list
But, when I add the .a file in my Build phases, it is displayed as red. I have tried all the method in SO, as well as other forums.
Is there any other way to upload image to Picasa.
Any help would be appreciated.
I don't have an app to test this with, but I've translated the php-curl solution from this SO question.
NSString *authCode = [NSString stringWithFormat:#"GoogleLogin auth=",#"ACCESS CODE FROM OAUTH2"];
NSString *userId = #"USER ID GOES HERE";
NSString *albumId = #"ALBUM ID GOES HERE";
NSString *albumUrl = [NSString stringWithFormat:#"https://picasaweb.google.com/data/feed/api/user/%#/albumid/%#",userId, albumId];
// To get the data from a PNG file
NSData *postData = UIImagePNGRepresentation([UIImage imageNamed:#"YOUR IMAGE"]);
// To get the data from a JPEG file
//NSData *postData = UIImageJPEGRepresentation([UIImage imageNamed:#"YOUR IMAGE"], 0.9f);
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
// Init and set fields of the URLRequest
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:#"POST"];
[request setURL:[NSURL URLWithString:[NSString stringWithString:albumUrl]]];
[request setValue:#"image/jpeg" forHTTPHeaderField:#"Content-Type"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"2" forHTTPHeaderField:#"GData-Version"];
[request setValue:authCode forHTTPHeaderField:#"Authorization"];
[request setHTTPBody:postData];
NSURLResponse* rep = nil;
NSError *err = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&rep error:&err];
if (data && !err) {
//Do something with the response
}
Hope this helps.

Error with GET method in JSON

I am receiving the unauthorized BBVA APIData error. The code Im using in Xcode as Objective-C for get the data is this:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:#"https://api.bbva.com/apidatos/zones/cards_cube.json?date_min=201301&date_max=201307&group_by=week"]];
[request setHTTPMethod:#"GET"];
[request setValue:#"application/json;charset=UTF-8" forHTTPHeaderField:#"Authorization: Y2hvbWFsaS1hcHA6MGM2ZTljNjgxODNiZmUzOWY2OGJiYjY1NjZlZmU4ZTE3MzI0NTcyOQ=="];
NSURLResponse *response;
NSData *GETReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSString *theReply = [[NSString alloc] initWithBytes:[GETReply bytes] length:[GETReply length] encoding: NSASCIIStringEncoding];
NSLog(#"Reply: %#", theReply);
This is the documentation I used: https://developer.bbva.com/api/api-datathon/section/api-reference
I would think that, in line 4, it should be
[request setValue: Y2hvbWFsaS1hcHA6MGM2ZTljNjgxODNiZmUzOWY2OGJiYjY1NjZlZmU4ZTE3MzI0NTcyOQ== forHTTPHeaderField:#"Authorization:"];
You might have to remove the colon after "Authorization:"

How to submit text messages to server via ios apps

my problem is I cannot send the text messages to server. Im using asihttprequest. I have define the USERNAME, UUID, PASSWORD, API_PASSWORD and NUMBER myself. What I want to do is just send the data to the server url given. Here is my code:
- (IBAction)sendClicked:(id)sender {
[sendButton resignFirstResponder];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:#"https://cc.frifon.net_dosmssend/"]];
[request setDelegate:self];
[request setNumberOfTimesToRetryOnTimeout:3];
[request setRequestMethod:#"POST"];
[request setPostValue:USERNAME forKey:#"sip"];
[request setPostValue:PASSWORD forKey:#"pwd"];
[request setPostValue:UUID forKey:#"uuid"];
[request setPostValue:API_PASSWORD forKey:#"key"];
[request setPostValue:messageText forKey:#"message"];
[request setPostValue:NUMBER forKey:#"to"];
[request setPostValue:#"Submit" forKey:#"submit"];
[request start];
nil;
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request {
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
messageText.text = response;
}
}
Try this,
- (IBAction)sendClicked:(id)sender {
[sendButton resignFirstResponder];
NSString *serviceString = https://cc.frifon.net_dosmssend/;
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:[serviceString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]];
[request setDelegate:self];
request.requestMethod = #"POST";
request.timeOutSeconds = 30.0;
[request addPostValue:USERNAME forKey:#"sip"];
[request addPostValue:PASSWORD forKey:#"pwd"];
[request addPostValue:UUID forKey:#"uuid"];
[request addPostValue:API_PASSWORD forKey:#"key"];
[request addPostValue:messageText forKey:#"message"];
[request addPostValue:NUMBER forKey:#"to"];
[request addPostValue:#"Submit" forKey:#"submit"];
[request startAsynchronous];
}
- (IBAction)sendClicked:(id)sender {
// Add you all data in below dictionary
NSDictionary *dictionary = #{#"action":#"login",#"nick_name":nikname,#"password":pass};
NSError *error;
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:&error];
NSString *jason =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"JSON summary: %#", jason);
NSString *address = [NSString stringWithFormat:#"%#",#"www.demo.com"];
address= [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *URL = [NSURL URLWithString:address];
NSLog(#"%#",address);
// request creation
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL cachePolicy:NSURLCacheStorageAllowedInMemoryOnly
timeoutInterval:60.0];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setHTTPBody:data];
responseData = [[NSMutableData alloc] init];
NSURLConnection *Conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[Conn start];
}

Resources