UIImage via https - iphone

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!

Related

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

Cocoa Error 256, while retrieving data from web server in Objective C

I have an application where I am sending image data on server through ftp and retrieving back in a functionality of preview. Now I am able to send the image on server,but while retrieving it back,I am getting following error in NSData and 0 bytes are fetched.
Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn\u2019t be completed. (Cocoa error 256.)"
I am parsing data through XML and fetching the image url like this
http://111.222.333.44/abc/images/User/img102-13.png
Now,when I use the following code to retrieve imgstr to nsdata, but getting the above error in the same.
NSURL *u = [NSURL URLWithString:imgstr];
NSError *error;
NSData *dt = [NSData dataWithContentsOfURL:u options:NSDataReadingMapped error:&error];
myImage = [UIImage imageWithData:dt];
I am able to fetch other data easily,but not the image data. Can anybody point out,where I am doing mistake or how I can resolve the problem? Thanks in advanced.
EDIT :
In my application I am first of all capturing an image and storing it on the server. And then on the 2nd page,I need to fetch the image data and to show the same image when the user clicks a button called Preview. I am having a web service where I am getting the data including the path of the image as shown above,but with the method shown I am not able to fetch the Image data in NSData and getting the Cocoa error 256. Hope this help someone to show me a better and correct Path. Thanks.
I think the problem with this approach is that it will block the UI thread until the image is downloaded.
try to load that image in separate thread.
Well i tried your code and i too have a webService that takes the image from a url.
instead of doing the above code. you can directly do
try this:
NSURL *u=[NSURL URLWithString:imgstr];
UIImage *image1 = [UIImage imageWithData:[NSData dataWithContentsOfURL:u]];
In my case its working.
If you are still struck then the below link might help you:
http://blog.beefyapps.com/2010/03/coredata-sqlite3-database-constraint-failed-error/

Loading Image from SQL Server DB Image Field into UIImageView

We have images stored in our SQL Server db in an image type field. Im just wanting to know how I go about getting them into a UIImageView in my iPad app. My iPad app talks to the SQL Server over an OData service provider so the image field comes across as type NSData so I tried the following code already:
UIImage *currentImage = [[UIImage alloc] initWithData:[currentEntityImage getEntityImageData]];
[myImageView setImage:currentImage];
Where "[currentEntityImage getEntityImageData]" is the NSData field converted from the image field. The problem is I can never get anything to display in the UIImageView.
Here is the corresponding code I use in our .net application that shows the image correctly:
Dim EntImage As EntityImage = CType(e.Entities.Item(0), EntityImage)
Dim ms As New System.IO.MemoryStream(EntImage.EntityImage)
Me.imgEntityImage.Image = System.Drawing.Image.FromStream(ms)
This works fine and shows the image correctly (i am accessing the same data on both platforms).
Any ideas on what im missing in my obj-c code? Ive been pulling my hair out all morning on this.
Thanks in advance
maybe the data returned from OData is encoded Base64.
Try decoding it with http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html
Hope it helps
Try this
NSData *imageData =[NSData dataWithData:[currentEntityImage getEntityImageData]];
UIImage *animalImage = [[UIImage alloc] initWithData:imageData];
[myImageView setImage:animalImage];

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.

Problem uploading image on multi-part form via iPhone

I am trying to write an iPhone app that will allow me to upload a jpg via multi-part form.
Sometimes, the uploaded photo looks corrupted on the server with weird pixels and the photo looks cropped.
What could be the reason for that?
How come I don't get a http error response from the server? (upload backend is written in PHP)
You can use base64 encoding like shown below
UIGraphicsBeginImageContext(CGSizeMake(145.0, 145.0));
[takephoto.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *LargeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *data = UIImageJPEGRepresentation(LargeImage, 0.25);
NSString *strImageFinalBASE64 =[data base64Encoding];
takePhoto can be your UIImageView:
IBOutlet UIImageView *takephoto;