Saving an image from the internet to iPhone - 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

Related

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!

iPhone - Retrieve NASA picture of the day

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.

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

Using UIImagePNGRepresentation

I'm trying to save a UIImageView image to a png file, and then use that file as a GLTexture.
I'm having trouble with this, when I try to run my code in the simulator, Xcode says it succeeded, but the simulator crashes with no error messages.
Here is my code:
NSString *dataFilePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/Picture.png"];
NSData *imageData = UIImagePNGRepresentation(imageView.image);
[imageData writeToFile:dataFilePath atomically:YES];
// Load image into texture
loadTexture("Picture.png", &Input, &renderer);
Judging by your reply to Metalmi, it looks like you're not giving us the actual code you're compiling. If you're actually doing what it looks like you're doing, then you're trying to write data to your application's bundle, which is going to fail because you're not allowed to do that. Can you provide us with the exact code you're trying to execute?
Try this:
NSString *dataFilePath = [[NSBundle mainBundle] pathForResource:#"Picture" ofType:#"png"];