iPhone - Retrieve NASA picture of the day - iphone

I would like to use NASA picture of the day in my application.
I have searched a lot how to retrieve it, but so far I found no answer.
Does anyone have any idea how I can do it?
Thanks,
Andrei

1) Find the URL for the NASA picture of the day's image file.
2) run this code:
NSString *NasaUrlStr = #"http:/nasa/imageoftheday.jpg"; // fill in correct path here
NSURL *imageURL = [NSURL URLWithString:NasaUrlStr];
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
self.myImageView.image = img;
If you want to get "fancy" you could run the "[NSData dataWithContentsOfURL:imageURL]" in a background thread so you don't stall your user interface while the image is downloading.

Related

Request to fetch me/picture or userId/picture fails with latest Facebook iOS SDK

With older versions of the Facebook SDK for iOS, I could ask for me/picture or userId/picture to fetch profile pictures. I'm getting a null response when I try this with the latest SDK.
Any idea why, and what's the best alternative if the API has changed?
Thanks!
Actually, you can still use [facebook requestWithGraphPath:#"me/picture?type=large" andDelegate:self];
But in -(void)request:(FBRequest *)request didLoad:(id)result, instead of using the value in result, get the image data of request.responseText, which is NSMutableData and build your image from there.
Example:
-(void)request:(FBRequest *)request didLoad:(id)result {
NSLog(#"Got user avatar");
UIImage *profileAvatar = [UIImage imageWithData:request.responseText];
UIImageView *avatarImageView = [[UIImageView alloc] initWithImage:profileAvatar];
[self.view addSubview:avatarImageView];
}
Credits: https://stackoverflow.com/a/10528707/982913
I had the same problem, I guess the changed something.maybe they want the developers to download the picture in "regular" way.
In the hackbook facebook sample code that come with the new SDK they download the picture like regular picture and not through request object :
NSString * url = [NSStringstringWithFormat:#"https://graph.facebook.com/%#/picture",facebookObjectIdString];
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
UIImage *downloadedImage = [UIImage imageWithData:data];
As you Can See, Just download the image from the URL like any image.
You can also download with NSURLRequest and sendSynchronousRequest etc...
Interesting to know if this is bug in the new SDK or decision.
update:
on some cases you want to add access_token parameter if the picture belong to some facebook object that restrict the access of downloading the picture (like some page's pictures).

Saving an image from the internet to iPhone

I'm trying to download an image from the internet. I'm not sure why this code is not working when I want to get the photos with these specs (209x209 pixels,not more than 65KB).
Writing to iPhone directory:
NSData *imgAsData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
//UIImage *image = [UIImage imageWithData:[NSData thumnail_url]];
NSFileHandle* myFileHandle = [NSFileHandle fileHandleForWritingAtPath:self.product.img_url];
[myFileHandle writeData:UIImagePNGRepresentation(image)];
[myFileHandle closeFile];
I even tried writeToFile, but it still doesn't work.
[imgAsData writeToURL: [NSURL URLWithString:self.product.img_url] atomically:YES];
Reading from iPhone directory:
NSFileHandle* myFileHandle = [NSFileHandle fileHandleForReadingAtPath:self.product.img_url];
//NSFileHandle* myFileHandle = [NSFileHandle fileHandleForReadingAtPath:self.product.thumbnail_url];
UIImage* loadedImage = [UIImage imageWithData:[myFileHandle readDataToEndOfFile]];
I was using this code in the same app to download thumbnails and they are working (notice the commented out lines using the "thumbnail_url"). I tried to substitute the thumbnail url in the place of this "larger" image and it worked. I was able to save the thumbnail offline, but I need to get the larger version (since, of course, the thumbnails are too pixelated).
I'm sure that the paths are ok since I got the thumnails working.
I'm lost. Is this not working because the file size is "larger"? Help please, Thanks!
The problem is NSFileHandle fileHandleForWritingAtPath can not create a file. You need to create the file first.
To save images to iPhone simply use
UIImageWriteToSavedPhotosAlbum(<#UIImage *image#>, <#id completionTarget#>, <#SEL completionSelector#>, <#void *contextInfo#>)
If you want to save images only 209*209 then add a check before that on the dimension of an image. I don't know(yet) how to read the images from iPhone, but you can use UIImagePickerController to choose the images.
Hope it helps

Load image from a stored png

How does one load or fetch an image that was saved to disk?
I wrote the image to the documents filepath using NSData and a PNG representation. How does one get this image back? UIImage.
UIImage *theImage = [UIImage imageWithContentsOfFile:filePath];
As ever, see the UIImage documentation at Apple. It is your friend.

UIImage via https

I am trying to create a UIImage with
NSData *testData = [NSData dataWithContentsOfURL:url];
while the url looks like
https://myimage.gif
So, if I try to access the image via my firefox, it exists, so the url is valid. I do a check which looks somehow like
if(testData)
and log sth. into console, so it shows me, there is some data. However, creating an image like this doesn't work:
UIImage *img = [UIImage imageWithData:testData];
I have no idea, what might be going wrong - appreciate any help!

How can we pull images from a website and display in our app using iphone sdk?

How can we pull images from a website and display in our app using iphone sdk?
Also, how can we embed audio in our iPhone App using iPhone SDK?
Can you please refer to any tutorial / videos related to this?
As #PeteRossi said, you can use this code:
NSData * data = [NSData dataWithContentsOfURL:urlOfImage];
UIImage * image = [UIImage imageWithData: data];
However, I would look into using ASIHTTPRequest for downloading things from the web. ASIHTTPRequest contains powerful classes for downloading things asynchronously so that your app won't have to wait for that image to download before it continues.
Pulling an image from a website:
NSData * data = [NSData dataWithContentsOfURL:urlOfImage];
UIImage * image = [UIImage imageWithData: data];
As for your second question, it depends on what you are trying to do. Here is an overview:
http://developer.apple.com/technologies/ios/audio-and-video.html