How to load image from NSMutableArray - objective-c

I want to load the image URL from my NSMutableArray.
Here is my Code:
id path = (NSString *)[[stories objectAtIndex: storyIndex] objectForKey: #"icon"];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data cache:NO];
If I use: id path = #"http://www.xzy.de/icon.png"; it´s all right, but not if I want to extract the imageURL from my Array
Anyone who can help me?
Thanks!

I don't get why you declare path as anonymous id type. Define it as NSString. Shouldn't be any problems.

Okay, here's the solution to the problem:
path = [path stringByReplacingOccurrencesOfString:#" " withString:#""];
path = [path stringByReplacingOccurrencesOfString:#"\n" withString:#""];
// that's a tab in the next line's string
path = [path stringByReplacingOccurrencesOfString:#" " withString:#""];

Related

facebook profile picture url not passing in NSURL

i am trying to pass profile pic url which is in the formate
https://fbcdn-profile-a.akamaihd.net/hpe-ak-xtp1/v/t1.0-1/c1.0.720/p7720/1364291238051_6773172187152486924_n.jpg?oh=6fda2708e5c1a5de47263&oe=585DC8CF&__gda__=1482408d4fc8e65486e8e1c269af042
but when i pass it like
NSString *fbpicurl = #"https://fbcdn-profile-a.akamaihd.net/hpe-ak-xtp1/v/t1.0-1/c1.0.720/p7720/1364291238051_6773172187152486924_n.jpg?oh=6fda2708e5c1a5de47263&oe=585DC8CF&__gda__=1482408d4fc8e65486e8e1c269af042";
NSString *post =[[NSString alloc] initWithFormat:#"profilepic=%#",fbpicurl];
NSURL *url=[NSURL URLWithString:#"http://URL.com"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
when i pass it this way url do not pass completely it just cut of where if founds & right after oh=6fda2708e5c1a5de47263 please help me how can i pass complete url string.
First encode your url string.
+ (NSString *) addPercentEscapesAndReplaceAmpersand: (NSString *) yourUrlString
{
NSString *encodedString = [yourUrlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
return [encodedString stringByReplacingOccurrencesOfString: #"&" withString: #"%20"];
}
Then used string for url by using NSURL URLWithString

objective-c google static map returns nil

this is my code:
#define GOOGLE_STATIC_MAP_IMAGE #"https://maps.googleapis.com/maps/api/staticmap?center=%#&zoom=15&size=320x150&maptype=roadmap&markers=color:green|label:H|%#"
-(UIImage*)GetGoogleStaticMap
{
NSString* coordinates = [NSString stringWithFormat:#"%f,%f", latitude, longitude];
NSString *stringURL = [NSString stringWithFormat:GOOGLE_STATIC_MAP_IMAGE, coordinates, coordinates]; //coordinates x2 = for the center & the marker
NSURL *url = [NSURL URLWithString:stringURL];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData];
return image;
}
if I paste stringURL to the browser i get the desired result, but imageData returns nil - ideas?
It may have returned nil because of certain characters in the string.
Try calling stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding on your stringURL before creating the url object.
For example:
newStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Not able to display images from particular directory IOS

I'm trying to create image from this location :
NSString *documentsCacheDirectory = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *fileName = #"image1.jpg";
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsCacheDirectory, fileName];
So applying to my Image :
UIImage *myImage = [UIImage imageNamed:filePath];
..setting this image as background to my button etc. etc.
But I'm not seeing any images displayed. However when I use the same technique for reading a file :
NSString *documentsCacheDirectory = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
NSString *fileName = #"file.txt";
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsCacheDirectory, fileName];
NSString *fileContents = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
for (NSString *line in [cacheFileContents componentsSeparatedByString:#"\n"]) {
.....
}
Reading file works just fine, but image does not, any reasons why?
imageNamed: is for images that are bundled with your app, you don't use this with a path. Use imageWithContentsOfFile: instead.

UIImage error with dynamic string

I'm trying to load an image into the cell,
The following code is working correctly :
NSString *imageURL = #"http://localhost/images/image2.jpg";
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imageURL]];
UIImage *rowImage = [[UIImage alloc] initWithData:imageData];
cell.image=rowImage;
but when i change imageURL to be dynamic , the image isn't displayed :
NSString *imageURL = [[stories objectAtIndex:indexPath.row] objectForKey:#"image"];
You might be saying the problem is in :
[[stories objectAtIndex:indexPath.row] objectForKey:#"image"];
But it's not , i've tried to log it and the output is as expected :
NSSLog(#"%#",[[stories objectAtIndex:indexPath.row] objectForKey:#"image"]);
//Outputs http://localhost/images/image2.jpg
What could be wrong ?
I am guessing there is space before or after the string. So do this,
NSString *imageURL = [[stories objectAtIndex:indexPath.row] objectForKey:#"image"];
imageURL = [imageURL stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:imageURL]];
UIImage *rowImage = [[UIImage alloc] initWithData:imageData];
While this should mostly fix your issue, I suggest you trim the strings before adding to the dictionary during the XML parsing.
You should also ensure you release both imageData and rowImage. You might be doing it already but its not in the code above so I just wanted to indicate that it could be leaking.
try this one
NSString *imageURL =[NSString stringWithFormat:#"%#",[[stories objectAtIndex:indexPath.row] objectForKey:#"image"]];

Crash on writeToFile

I'm using quite a simple method of storing file names in a text file. For some reason when I initiate the writeToFile I get a crash:
pathString = [NSString stringWithFormat:#"New FileName - %#.png", identifier];
NSString *currentContents = [NSString stringWithContentsOfFile:saveFilePath encoding:NSUTF8StringEncoding error:nil];
NSString *newContents = [NSString stringWithFormat:#"%#:::%#",currentContents, pathString];
NSData *newData = [newContents dataUsingEncoding:NSUTF8StringEncoding];
[newData writeToFile:saveFilePath options:NSDataWritingAtomic error:nil];
It reads the file, places it's contents into a variable called currentContents, then adds the new string to the file, and re-writes it. What's going wrong here.
Without the writeToFile line it works, with it, I get a crash.
Origin of saveFilePath
NSString *saveDocument = [NSString stringWithFormat:#"SavedFile.txt"];
NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
saveFilePath = [docsDirectory stringByAppendingPathComponent:saveDocument];
An NSLog of saveFilePath reveals a correct path
I think your problem might actually be a missing null character ('\0') at the end of your NSData object. So you finally end up with messed up data. You might want to use -writeToFile:atomically:encoding:error: on your new string right away anyway.
It turns out that the reason the file wasn't writing was because of an unallocated variable:
NSString *currentContents = [NSString stringWithContentsOfFile:saveFilePath encoding:NSUTF8StringEncoding error:nil];
should have been:
NSString *currentContents = [[NSString alloc] initWithContentsOfFile:saveFilePath encoding:NSUTF8StringEncoding error:nil];