Loading Image from SQL Server DB Image Field into UIImageView - iphone

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];

Related

How to convert all image format into .png format?

I am making a small application where I am receiving images from web-service. I am getting the image url. And the format of images is different different like .jpg,.gif,.png etc. And I want to save these images into my sandbox and later on show it into image view.
To do this I am giving a self name and gave the extension to .png. But at the time of retrieving the .png images are shown but different one's are not shown. So can you give me any idea how to convert all images to .png format or any other approach to do this?
after you retrieve your images/url save them this way:
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:YES];
Would it not be easier if you use an image library like SDWebImage which handles all the asynchronous image downloading and caching of the image onto your device?
https://github.com/rs/SDWebImage
Then you can do something like:
[myImageView setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];

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/

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;

Display an image that's stored locally

I'm using the following code from Three20 to display a photo/image gallery. Can anyone help with changing the code to display an image that's locally stored on the phone instead of on a web server?
[[[MockPhoto alloc]
initWithURL:#"http://farm4.static.flickr.com/3444/3223645618_13fe36887a_o.jpg"
smallURL:#"http://farm4.static.flickr.com/3444/3223645618_f5e2fa7fea_t.jpg"
size:CGSizeMake(320, 480)
caption:#"This is a caption."] autorelease],
you can make a data base table on which you store the image after been changed to an NSData object

How to insert image in xml on iphone

In my app i insert images in the tableview by putting the url of image in the field image of xml!!
It's possible insert the entire image directly in XML, including in the field image, instead of the URL, a kind of image compression, for example, the bits that make up the image.
This will increase the loading time a lot!
Search for a solution for "lazy loading" on the Web.
This is the most common approach for loading images in the table views on iPhone...
EDIT:
If you still want to use blub (image data as text inside the XML) then you might use the next code sample:
NSData *imageData = [NSData dataWithBase64EncodedString:imageString];
UIImage *image = [UIImage imageWithData:imageData];