downloading data Json pictures - iphone

hi this is a part of my code
NSURL *jsonURL = [NSURL URLWithString:#"http://*****.php"];
NSString *donneeJson = [[NSString alloc] initWithContentsOfURL:jsonURL];
self.pseudoOnline = [donneeJson JSONValue];
NSDictionary *json2 = [pseudoOnline valueForKey: #"photo"];
NSLog(#"adresse photo%#",[pseudoOnline valueForKey: #"photo"]);
NSString *url = [[pseudoOnline valueForKey: #"photo"]objectAtIndex:0];
NSLog(#"adresse photo%#",url );
adresse photo(
"http://************/XWsmG18O27.jpg",
"http://************/Wz0ab6oIxU.jpg",
"http://***********/9yKzQrpO59.jpg",
"http://************/l2rbNeIK8a.jpg")
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
how i can download all the pictures because iam only take picture index0 !
thks
thks

ads2 is an instance of NSArray, not NSDictionary. NSArray is not a key-value collection and does not respond to valueForKey:

Related

how to capture data through web service (php) sql query select with condition from ios iphone sdk

I have the following php file:
<?php
$username="root";
$database="testdb";
mysql_connect(localhost,$username);
$user=$_GET["user"];
$password=$_GET["password"];
$query="SELECT documento FROM person WHERE user='".$user." and password ='".$password."'";
$result=mysql_query($query) or die (mysql_error("error "));
$num=mysql_numrows($result);
mysql_close();
$rows=array();
while($r=mysql_fetch_assoc($result)){
$rows[]=$r;
}
echo json_encode($rows);
?>
to retrieve the information achieved by the following functions but the example did not have the php input conditions and parameters was select * from person here the functions
based in http://www.youtube.com/watch?feature=endscreen&v=IQcLngIDf9k&NR=1
-(void) getData:(NSData *) data{
NSError *error;
json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
}
-(void) start {
NSURL *url = [NSURL URLWithString:kGETUrl];
NSData *data = [NSData dataWithContentsOfURL:url];
[self getData:data];
}
now as the sql statement has input parameters is not as code and tried something like this A Simple PHP/MySQL Web Service for iOS but I can not accommodate what I need.
Pass the parameters in the string. Here is an example
-(void) start {
NSString *user = [[NSString alloc] initWithString:#"exampleuser"];
NSString *password = [[NSString alloc] initWithString:#"examplepassword"];
NSString *urlstr = [[NSString alloc] initWithFormat:#"http://myserver.com/myphpfile.php?user=%#?password=%#", user, password];
NSURL *url = [NSURL URLWithString:urlstr];
NSData *data = [NSData dataWithContentsOfURL:url];
[self getData:data];
}
As it is password and user it normally is better to use POST instead of GET because then it is not part of the URL which is visible. With POST you can hide it if you have a https line. But I think this is not your main concern right now. The above should work.
I made some change in Hollfeder's code and it worked perfectly :)
-(void) start {
NSString *user = [[NSString alloc] initWithString:#"exampleuser"];
NSString *password = [[NSString alloc] initWithString:#"examplepassword"];
NSString *urlstr = [[NSString alloc] initWithFormat:#"http://myserver.com/myphpfile.php?user=%#&password=%#", user, password];
NSString *urlstr_encoded = [urlstr stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [NSURL URLWithString:urlstr_encoded];
NSData *data = [NSData dataWithContentsOfURL:url];
[self getData:data];
}

Passing uiimage to new view

I am trying to create an array of images (from the web) and then pass this array to a new view. Here is the code I'm using:
WebViewController *webController = segue.destinationViewController;
NSURL *url = [NSURL URLWithString:[gift objectForKey:#"smallImageUrl"]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
webController.image = image;
Also in my WebViewController.h i have:
#property(strong,nonatomic) UIImage *image;
But when I try to use "image" in WebViewController.m all i get is a (null) value. Where am I going wrong? Thanks!
UPDATE:
The problem appears to not be as I stated before. The image makes it to WebViewController. I am trying to save the image to an array which must be where my problem lies. This is the code I have for that in a saving method (in WebViewController.m):
NSMutableArray *imageArray = [[NSMutableArray alloc] init];
[imageArray addObject:image];
[imageArray writeToFile:imageFileName atomically:YES];
also in my viewdidload i do:
paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
documentsDirectory = [paths objectAtIndex:0];
imageFileName = [NSString stringWithFormat:#"%#/imageArray",documentsDirectory];

Not getting images in document directory folder

I want to get images in document directory folder.
I am using this code.
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
for(int i=0;i<[imagepath count];i++)
{
NSString* documentsDirectory = [path objectAtIndex:0];
NSURL *img = [NSURL URLWithString:[imagepath objectAtIndex:i]];
//here imagepath is array in that i get the url of image.
//here when i print the img data i got url from where i have to get images.
NSData *data = [NSData dataWithContentsOfURL:img options:NSDataReadingMapped error:nil];
//here when i put NSLog i get the null.
NSString *fullImagePath1 = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"image%d.png",i]];
[newreturnImage addObject:fullImagePath1];
[data writeToFile:fullImagePath1 options:NSDataWritingAtomic error:nil];
}
I can't get images in document directory folder.
What could be wrong with this code?
Try this code,
NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", docDirectory,#"myimage.png"];
UIImage *cellImage=[UIImage imageWithContentsOfFile:filePath];
UIImageView *temp=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
temp.image=cellImage;
[myView addSubview:temp];
[temp release];
Updated :
NSData *myData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:myURL]];
UIImage *myImage = [[UIImage alloc] initWithData:myData];
This link may help you. In the link James Webster has provided an excellent method which suits your requirement. If this does not help you. The code below that would definitely work for you as it works for me.
How save images in home directory?
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
for(int i=0;i<[imagepath count];i++)
{
NSString* documentsDirectory = [path objectAtIndex:0];
NSURL *img = [NSURL URLWithString:[imagepath objectAtIndex:i]];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:img];
NSString *fullImagePath1 = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"image%d.png",i]];
[newreturnImage addObject:fullImagePath1];
[data writeToFile:fullImagePath1 options:NSDataWritingAtomic error:nil];
}
Hope this helps you.
Thanks.

Can't get Images from the iPhone device folder

I'm trying to get some images from the device (iPhone) and show them on screen.
The image files are saved on my device and separated in folders like this: (...)/Documents/1/image1.png.
Here is the code of the function where I'm trying to get the image
-(UIImage *) getImageFromDevice:(NSString *) fileName
idImage:(int) idImage{
NSString *path;
path = [[self dataFilePath] stringByAppendingString:#"/"];
path = [path stringByAppendingString: [[NSString alloc] initWithFormat:#"%d",idImage]];
path = [path stringByAppendingString:#"/"];
path = [path stringByAppendingString:fileName];
NSLog(#"path = %#", path);
NSURL *deviceImageUrl = [[NSURL alloc] initWithString:path];
NSData *imageData = [NSData dataWithContentsOfURL:deviceImageUrl];
UIImage *deviceImage = [UIImage imageWithData:imageData];
return deviceImage;
}
And this is the function to get the path to the device folder that's working fine:
-(NSString *)dataFilePath{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;
}
But when I set the NSURL *deviceImageUrl with [[NSURL alloc] initWithString:path] the deviceImageUrl becomes nil.
What I'm doing wrong?
//NSURL *deviceImageUrl = [[NSURL alloc] initWithString:path];
//NSData *imageData = [NSData dataWithContentsOfURL:deviceImageUrl];
//UIImage *deviceImage = [UIImage imageWithData:imageData];
UIImage *deviceImage = [[[UIImage alloc] initWithContentsOfFile:path]autorelease];
Try using fileURLWithPath: instead of URLWithString: ;)
NSURL *deviceImageUrl = [NSURL fileURLWithPath:path]; // autoreleased url
I always mess up with these two methods, too ... they're making me crazy.
You should not use initWithString: , this must conform to URL format as described in RFCs. If you just want to use a system path, use this : (don't forget to release after use it)
NSURL *deviceImageUrl = [[NSURL alloc] initFileURLWithPath:path isDirectory:NO];

want to extrac image from string url

I have the following image xml tag in which image path is stored
NSString *imgstring =[[blogEntries objectAtIndex: blogEntryIndex1] objectForKey: #"image"];
now i want to display this image in UITAble cell by following method
cell.textLabel.text=imgstring;
i am just getting path on the cell not actual image how should i display image
Of course you're getting text displayed - you're setting the text property of a UILabel to a string :) How would it know you wanted it to display an image?
You want something like
cell.imageView.image = [UIImage imageNamed:imgstring];
Have a look at the UITableViewCell documentation for more details
However, this will only work for images in your project - for external images you will have to do a little more work.
NSString *imgstring =[[blogEntries objectAtIndex: blogEntryIndex1] objectForKey: #"image"];
NSURL *url = [NSURL URLWithString:imgstring];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
However, this will block your app until the image is loaded. Instead you might need to load the images is the background :
// Tell your code to load the image for a cell in the background
NSString *imgstring =[[blogEntries objectAtIndex: blogEntryIndex1] objectForKey: #"image"];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:cell, #"cell", imgstring, #"imgstring", nil];
[self performSelectorInBackground:#selector(loadImage:) withObject:dict];
- (void) loadImage:(id)object {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Get the data to pass in
NSDictionary *dict = (NSDictionary *)object;
// Get the cell and the image string
NSString *imgstring = [dict objectForKey:#"imgstring"];
UITableViewCell *cell = [dict objectForKey:#"cell"];
NSURL *url = [NSURL URLWithString:imgstring];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
[pool release];
}
But you're going to run into threading problems if you're not careful with this :)
If you get any other problems, post another comment and I'll try to help!
PS I've not tested this code, just typed it straight in so there might be typos, sorry!