Create an UIImage from NSString - nsstring

my problem is: i get from webservices an image. this is stored in NSString. and now i want show it as UIImage. But I dont know how to do it.
how i can convert data from NSString in NSData?
thanks in advance :)

you can use the below methods for converting string to data
[NSData dataFromBase64String:yourString];
[NSData dataFromBase64EncodedString:yourString]

Related

Error decoding image data

I receive url path from a meteor server and trying to display it. but onError logs
Error decoding image data <NSData 0x7f9d820c5a00; 1196 bytes>
I log the path and view it on browser and its there. what's causing it? or at least what does this error mean?
Edit:
I'm using meteorjs for server. I think its has something to do with it. I use static images online and it works just fine.
PS: I dont code in objective C
I think, you are directly displaying NSData without converting in to image. Do like this:
NSData *data = [NSData datawithContentsOfURL:URLString];
UIImage *image = [UIImage imageWithData:data];

NSString from NSData -- Server returns NSData with leading nulls

I haven't seen a very clear answer on this one. For some reason I don't understand, the server I am receiving data from returns a JSON response with a lot (hundreds, maybe) of null bytes at the beginning.
When I use the following code, the string appears to be null:
NSString* newStr = [[[NSString alloc] initWithData:dataToBeLoaded encoding:NSUTF8StringEncoding] autorelease];
This is apparently a common problem, but I haven't seen a clear answer on how to convert the NSData to an NSString.
Up until recently, I saw this problem with an intermediate NSString value, but it went away after I stripped a handful (but not hundreds) of characters off of the beginning and end. The NSString after stripping was perfectly fine, so I suppose the encoding method was not the problem.
Is there a built-in method that will do this properly? I have to imaging it would be more efficient that code I would write to go through byte-by-byte.
By the way, when I manually send the JSON request to the server in my browser, the response comes back as perfectly valid JSON, with no obvious problems showing up in the browser screen.
Any ideas what I should do? Thanks.
The data that is being returned from each server contains valid JSON (verified with JSON Lint in many instances). In one case, the return data is a javascript assignment statement containing a JSON object. In another case, it's simply a JSON object. In both cases, I know that the JSON object begins with a curly bracket.
So, to strip off all of the leading nulls, I use the code below. It seems that some methods treat the string data in an NSString like a C string, but other methods are aware that there is more to it, including an awareness of the length. Fortunately, rangeOfString and substringFromIndex look beyond the leading nulls.
NSString* newStr = [[[NSString alloc] initWithData:dataToBeLoaded encoding:NSUTF8StringEncoding] autorelease];
if (newStr == nil) return;
NSRange headRange = [newStr rangeOfString:#"{"];
NSString *stripped = [newStr substringFromIndex:headRange.location];

iPhone to MS SQL Image Data Type Conversion Question

Alright, here we go!
I am currently developing an iPhone app that will work as a security check-in system for events at a church. The main goal is to be able to search a pre existing MS SQL database for a participant, and then save them to a list on the iPhone/iPod Touch/iPad. Easy enough. One of the fields in the MS SQL database is of Image data type, and its function is to store the participant's image in binary form. I currently am not allowed to switch to varbinary(MAX).
From the iPhone, I use:
UIImage *image = [UIImage imageNamed:#"nameOfImage.jpg"];
NSData *imageData = UIImageJPEGRepresentation(image,1.0);
NSString *imageString = (NSString *)[[[NSString alloc] init] base64StringFromData:(NSData *)imageData];
You may be wondering what the base64StringFromData is. It's a category discussed here, How do I do base64 encoding on iphone-sdk?
In the category, it returns a string as NSASCIIStringEncoding. I then send that string to a Visual Basic web service using POST.
Assume in the following that I have already set up and opened my SQL connection, and initlialized a SQL Data adapter and command. So the function looks similar to this:
Public Function sendCommand(ByVal image As String) As String
Dim commandString As String
commandString = "insert into phoneApp(image) values(#image)"
Dim encoding As New System.Text.UTF8Encoding
sqlDataAdapter.InsertCommand = (commandString, sqlConnection)
sqlDataAdapter.InsertCommand.Parameters.Add(#"image", SqlDbType.Image, image.length).Value = encoding.GetBytes(image)
sqlDataAdapter.ExecuteNonQuery()
End Function
Now, finally, here's what is happening. In another function, I call Response.BinaryWrite(SqlDataReader.Item("image")). In Internet Explorer, doing this would then make the image appear in the browser. Instead, it displays the string in base64 form. Copy and pasting that string in a base64 converter off the net, and it works. So the encoding on the iPhone did encode to base64 properly.
Here's the question. Do I even need to be encoding to base64 to save into Image Data Type, or should I be focusing on learning how to encode the NSData into a different binary string?
I figured it out, and will leave my answer to anyone that stumbles across this. Good luck to you in your programming.
It actually was a lot simpler than I thought. Just use System.Convert to pass in the bytes on the InsertCommand.
sqlDataAdapter.InsertCommand = (commandString, sqlConnection)
sqlDataAdapter.InsertCommand.Parameters.Add(#"image", SqlDbType.Image, image.length).Value = System.Convert.FromBase64String(image)
sqlDataAdapter.ExecuteNonQuery()
In essence, I was getting the bytes for the encoding string but never decoding it further down the line. So I stopped the double encoding by using the Convert before it was inserted into the table.

convert to an UIImage (iPhone) a data stream from an asp.net web service

I developed an asp.net web service that send an image and i want to convert the received data stream to an UIImage in my iPhone
this is a sample of what i get from the web service
R0lGODlhbQCdAOYA...KsxbcSAAOw==
thank you in advance !
This is an base64 encoded string, so you need to first decode it and then put it into an NSData object.
The next thing is to create an UIImage from the NSData object, this can be done like this:
NSData *myData;
UIImage *image = [UIImage imageWithData:myData];
And in order to encode/decode Base64 encoded strings into NSData, see the following post. At the end there's a link to a NSData category that does Base64 encoding/decoding.

Conversion from string to double error

i have a project for uploading video.in that when i click the showvideo button there is a error.
code-
param name="url" value='<%# "VideoHandler.ashx?FileID=" + Eval("FileID") %>'
error message ::: conversion form string="VideoHandler.ashx?FileID=" to type 'Double' is not valid
anyone knows please answer for me thank you
"VideoHandler.ashx?FileID=" is a string. Eval("FileID") results in a double. You have a type mismatch, so the addition overload doesn't know how to proceed. Solve it like this:
string.Format("VideoHandler.ashx?FileID={0}", Eval("FileID"))
Without seeing the code, it sounds like you are trying to convert a string which isn't a valid double. Are you taking the value of the query string and trying to convert it or, could you have accidentally tried to convert the page name along with the query string? Based on the short error message you gave, that is what it looks like. If you post the code which is doing the conversion, that will likely make it clearer what is going on, but that is my best guess at the moment.
You are trying to convert a string which is not a valid double type.
I think you are trying to convert the FileID field to double. Then you can split the string and then convert only the FileID part of it.
You can get the querysting data using
Request.QuerySting["FileID"] and then convert it to double.
or use
Double.TryParse Method
We should see some code. Apparantly the application is trying to convert "VideoHandler.ashx?FileID=" to a Double value which cannot be done.
Just add ToString() to the end of your Eval. FileId is a double, and it's seeing the + and trying to add it to a string, numerically, instead of concatenating it.

Resources