views:

214

answers:

2

Hi ,

I am trying to download the image from the url http://a3.twimg.com/profile_images/414797877/05052008321_bigger.jpg

I am using the following code, but image is not saved. I want to know what i m doing wrong.

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://a3.twimg.com/profile_images/414797877/05052008321_bigger.jpg"]];
 [NSURLConnection connectionWithRequest:request delegate:self];

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:@"pkm.jpg"];
 NSData *thedata = NULL;
 [thedata writeToFile:localFilePath atomically:YES];

 UIImage *img = [[UIImage alloc] initWithData:thedata];
+1  A: 

If you set theData to nil, what do you expect it to write to the disk?

What you can use is NSData* theData = [NSData dataWithContentsOfURL:yourURLHere]; to load the data from the disk and then save it using writeToFile:atomically:. If you need more control over the loading process or have it in background, look at the documentation of NSURLConnection and the associated guide.

frenetisch applaudierend
That's exactly tony-blue required.
Jim
thank you very much frenetishch , my problem solved
tony blue
A: 

Hi It is clear that you are writing NULL data to your file.

In your code statement NSData *thedata = NULL; indicates that you assign NULL value to your data.

You are writing NULL data to your file as well.

Please check your code once again.

Thanks,
Jim.

Jim
Note that sending a selector to nil does not have any effect, so actually nothing is written to disk (not even null data). Does not really matter in this case, but just to point out the subtle difference.
frenetisch applaudierend
thanks jim, now my problem solved
tony blue