NSString* str = [inventoryDetails objectAtIndex:i];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(j, 10, 280, 180)];
if (![str isEqual:[NSNull null]]) {
NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:[[inventoryDetails objectAtIndex:i] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
UIImage *img=[[UIImage alloc]initWithData:data];
I'm getting output as
https:google.com/images/320.png
How to remove the space in url
Try this
NSString *str = #"http://...";
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Just do:
[str stringByReplacingOccurrencesOfString:#" " withString:#""]
You can trim your NSString if your space are on the sides.
NSString *trimMe = #" Trim this string ";
trimMe = [trimMe stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(#"Trimmed String: ++%#++", trimMe);
//returnes Trimmed String: ++Trim this string+++
Related
I'm trying to insert a base 64 image into UIImage in Objective-C I do the following:
I have the user's image into a NSURL
NSURL *url = [NSURL URLWithString: [fetchDefaults objectForKey:#"img"]];
Then I cast the url, into a NSString
NSString *string=[NSString stringWithFormat:#"%#",url];
Then I clean the string, and add the prefix "data:application/octet-stream;base64," also tried with "data:image/jpg;base64,"
NSMutableString *tempStr = [NSMutableString stringWithString:string];
[tempStr replaceOccurrencesOfString:#" " withString:#"+" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [tempStr length])];
NSString *temp = [[NSString stringWithFormat:#"data:application/octet-stream;base64,%#",tempStr] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
And finally the string is cast to an NSData to be inserted into UImage
NSData *dat = [[NSData alloc]initWithBase64EncodedString:temp options:NSDataBase64DecodingIgnoreUnknownCharacters];
[avatar setImage:[UIImage imageWithData:dat]];
Despite of value of dat is not nil, when I set the image to the UIImage the image isn't showed, any idea of what am I doing wrong?
From what I understand you have your base64 string in fetchDefaults.
/*Get base64 string*/
NSString *base64 = [fetchDefaults objectForKey:#"img"];
Use this NSData category: https://searchcode.com/codesearch/view/40028750/
/*Convert base64 to NSData object*/
NSData *data = [[NSData alloc] initWithBase64EncodedString:base64];
/*Convert data to UIImage object*/
UIImage *image = [[UIImage alloc] initWithData:data];
Try to pass my NSString to a URLWithString but no url appear:
//here i get my url from my API and replacing tags
NSString *queryContent = [[[(webLinks)[#"content"] stringByReplacingOccurrencesOfString:#"&" withString:#"&"]
stringByReplacingOccurrencesOfString:#"<p>" withString:#""]
stringByReplacingOccurrencesOfString:#"</p>" withString:#""];
//here i get the full url
NSURL * url = [NSURL URLWithString:queryContent];
Adding a NSLogs:
NSLog(#"query:%#", queryContent);
NSLog(#"website:%#", url);
and the result is this:
query:http://mywebsite.com
website:(null)
Whats wrong?
thanks
//SOLVED//
here the correct code if any of you use my same system JSON API for WordPress:
NSRange r;
NSString *queryUrl = [(webLinks)[#"content"] stringByReplacingOccurrencesOfString:#"\n" withString:#""];
NSString *website = queryUrl;
while ((r = [website rangeOfString:#"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
website = [website stringByReplacingCharactersInRange:r withString:#""];
NSURL * url = [NSURL URLWithString:website];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
How can I convert a NSUUID to NSString?
NSString *url = [self mysql_process:ESTIMOTE_PROXIMITY_UUID];
- (NSString*)mysql_process:(NSUUID *)beacon_id
{
NSString *strURL = [NSString stringWithFormat:#"http://path_to_php_file/mysql.php?id=%#", beacon_id];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
return strResult;
}
When I try to NSLog the URL im getting a pointer, which shows me my NSUUID, ESTIMOTE_PROXIMITY_UUID in this case.
NSUUID has a method: -(NSString*)UUIDString;.
So:
NSString *uuidString = [yourNSUUID UUIDString]
swift 3 version
Ex:
let uuidStringValue = "12345678-1111-2222-3333-4567891012B4"
let proximityUUID = NSUUID(uuidString: uuidStringValue)
If you want NSString from NSUUID. Use UUIDString of NSUUID...
NSUUID *uuidStringRef = [[UIDevice currentDevice] identifierForVendor];
NSString *stringRef = [uuidStringRef UUIDString];//you can replace uuidStringRef with your ESTIMOTE_PROXIMITY_UUID
Hope it helps you...!
NSString *url = [self mysql_process:ESTIMOTE_PROXIMITY_UUID];
- (NSString*)mysql_process:(NSUUID *)beacon_id {
**NSString *strDeviceUUID = [beacon_id UUIDString];** // Converts NSUUID to UUIDString format
NSLog(#"beacon_id : %# strDeviceUUID : %# ", beacon_id,strDeviceUUID);
NSString *strURL = [NSString stringWithFormat:#"http://path_to_php_file/mysql.php?id=%#", strDeviceUUID];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
return strResult;
}
This function is present in NSUUID.h file of Foundation frameworks.
/* Return a string description of the UUID, such as "E621E1F8-C36C-495A-93FC-0C247A3E6E5F" */
- (NSString *) UUIDString;
I have to convert unicode char to NSString
the text i have to convert is doc.title = "hello 64 \U00bf world sample"
my code is:
UILabel *lblName=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, 250, 88)];
const char *cString = [doc.title UTF8String];//where doc.title is above text
NSData *data = [NSData dataWithBytes:cString length:strlen(cString)];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
lblName.text= string;
NSLog(#"Title is = %# \n",doc.title);
lblName.font=[UIFont boldSystemFontOfSize:16];
lblName.numberOfLines=100;
lblName.backgroundColor=[UIColor clearColor];
lblName.textColor=[UIColor colorWithRed:2.0/255.0 green:104.0/255.0 blue:170.0/255.0 alpha:1.0];
[cell.contentView addSubview:lblName];
[lblName release];
where am i doing wrong?
Tried the link but could not get it right.
+(NSString *)getTextFromChar:(unichar)character
{
NSString *string = [NSString stringWithFormat:#"%C",character];
return string;
}
Pass your cString to this method
try this
char *cString[] = [doc.title UTF8String];//where doc.title is above text
NSData *data = [NSData dataWithBytes:cString length:strlen(cString)];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
lblName.text= string;
or simply call
-[NSString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES].
I erased the code between the lines, because it's nothing special, just adding a contact to an array. I think it has to do with the brackets. So hopefully someone has a good eye and can tell me, why its always adding a contact two times :)
if ([string rangeOfString:#"00"].location == NSNotFound) {
if(lastName && ([lastName length] > 0)) {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *ccode = [prefs stringForKey:#"cc"];
NSString *fileName = [prefs stringForKey:#"onumber"];
NSString *str = number;
NSString *newNumber = [str substringWithRange:NSMakeRange(1, [str length] - 1)];
NSString *Nummer = [newNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *strURL = [NSString stringWithFormat:#"http://myserver/phpFile.php?number=%#%#&name=%#&lastname=%#", ccode, Nummer, firstName, lastName];
NSString *webStringURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:webStringURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
}
else {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *ccode = [prefs stringForKey:#"cc"];
NSString *fileName = [prefs stringForKey:#"onumber"];
NSString *str = number;
NSString *newNumber = [str substringWithRange:NSMakeRange(1, [str length] - 1)];
NSString *Nummer = [newNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *strURL = [NSString stringWithFormat:#"http://myserver/phpFile.php?number=%#%#&name=%#&lastname=""", ccode, Nummer, firstName];
NSString *webStringURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:webStringURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
}
} else if(lastName && ([lastName length] > 0)) {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *ccode = [prefs stringForKey:#"cc"];
NSString *fileName = [prefs stringForKey:#"onumber"];
NSString *str = number;
NSString *newNumber = [str substringWithRange:NSMakeRange(0, [str length] - 0)];
NSString *Nummer = [newNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *strURL = [NSString stringWithFormat:#"http://myserver/phpFile.php?number=%#%#&name=%#&lastname=%#", ccode, Nummer, firstName, lastName];
NSString *webStringURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:webStringURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
}
else {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *ccode = [prefs stringForKey:#"cc"];
NSString *fileName = [prefs stringForKey:#"onumber"];
NSString *str = number;
NSString *newNumber = [str substringWithRange:NSMakeRange(0, [str length] - 0)];
NSString *Nummer = [newNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *strURL = [NSString stringWithFormat:#"http://myserver/phpFile.php?number=%#%#&name=%#&lastname=""", ccode, Nummer, firstName];
NSString *webStringURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:webStringURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
}
if ([string rangeOfString:#"+"].location == NSNotFound) {
if(lastName && ([lastName length] > 0)) {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *ccode = [prefs stringForKey:#"cc"];
NSString *fileName = [prefs stringForKey:#"onumber"];
NSString *str = number;
NSString *newNumber = [str substringWithRange:NSMakeRange(1, [str length] - 1)];
NSString *Nummer = [newNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *strURL = [NSString stringWithFormat:#"http://myserver/phpFile.php?number=%#%#&name=%#&lastname=%#", ccode, Nummer, firstName, lastName];
NSString *webStringURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:webStringURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
}
else {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *ccode = [prefs stringForKey:#"cc"];
NSString *fileName = [prefs stringForKey:#"onumber"];
NSString *str = number;
NSString *newNumber = [str substringWithRange:NSMakeRange(1, [str length] - 1)];
NSString *Nummer = [newNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *strURL = [NSString stringWithFormat:#"http://myserver/phpFile.php?number=%#%#&name=%#&lastname=""", ccode, Nummer, firstName];
NSString *webStringURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:webStringURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
}
} else if(lastName && ([lastName length] > 0)) {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *ccode = [prefs stringForKey:#"cc"];
NSString *fileName = [prefs stringForKey:#"onumber"];
NSString *str = number;
NSString *newNumber = [str substringWithRange:NSMakeRange(3, [str length] - 3)];
NSString *Nummer = [newNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *strURL = [NSString stringWithFormat:#"http://myserver/phpFile.php?number=%#%#&name=%#&lastname=%#", ccode, Nummer, firstName, lastName];
NSString *webStringURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:webStringURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
}
else {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *ccode = [prefs stringForKey:#"cc"];
NSString *fileName = [prefs stringForKey:#"onumber"];
NSString *str = number;
NSString *newNumber = [str substringWithRange:NSMakeRange(3, [str length] - 3)];
NSString *Nummer = [newNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
NSString *strURL = [NSString stringWithFormat:#"http://myserver/phpFile.php?number=%#%#&name=%#&lastname=""", ccode, Nummer, firstName];
NSString *webStringURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:webStringURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
}
if ([string rangeOfString:#"+"].location == NSNotFound) condition should start with else.
It should be as below:
else if ([string rangeOfString:#"+"].location == NSNotFound)
Also, before the second if line (mentioned below)
if ([string rangeOfString:#"+"].location == NSNotFound))
you should have closed '}' for first if.
If some of the omitted code is executing twice, it's because the construction of your strings is such that two of your if statements are both true for the same string. Examine your strings, post the one that is being added twice. Does one of them have a + and 00 in it? But your bracketing is fine.
UPDATE
I guess I would start hunting this down by adding NSLog() statements to each of the if/else blocks to see when each is called. If you have a remote web service, the error could be there as well. In fact, it might be -- your obj-c code looks ok to me. Track GET requests at the server and add logging to phpFile.php.