UIImage error with dynamic string - objective-c

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

Related

Unable to fetch data from nsurl

NSURL *url = [NSURL URLWithString:[self.imageArray objectAtIndex:indexPath.row]];
NSLog(#"My URL : %#",url);
NSData *data = [NSData dataWithContentsOfURL:url];
NSLog(#"New Data : %#",data);
UIImage *tmpImage = [[UIImage alloc] initWithData:data];
cell.imageView.image = tmpImage;
i think i got your problem, check this like my upload image in your project.
i also make a gif about how to add this setting in your project , just follow the step in my gif.

Syntax for combining NSURL and normal string?

What syntax could I use to make this work?
UIImage with NSURL
banner.image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.website.com/file.file"+placemark.administrativeArea+#".png"]]];
Thanks!
Use This way:
banner.image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.website.com/file.file%#.png",placemark.administrativeArea]]]];
Don't use so much statement in one line.
IT has several problem like Difficult to understand and Hard to find error.
I suggest this way:
NSString *imgStr = [NSString stringWithFormat:#"http://www.website.com/file.file%#.png",placemark.administrativeArea];
NSURL *imgURL = [NSURL URLWithString:imgStr];
NSData *imgData = [NSData dataWithContentsOfURL:imgURL];
UIImage *img = [[UIImage alloc] initWithData:imgData];
banner.image = img;
But it depend on u.
Good day..

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.

Nothing displayed after splitting a list of image URLs

I have six image URLs in a string named aux. I split it with component separated by newlines. I want to pick images one by one from aux and display each in an ImageView. My code is:
aux = [_homeText.text stringByAppendingString:[NSString stringWithFormat:#" %# ",item.shortDescription]];
[_homeText setText:aux];
NSString *list =aux;
NSArray *listItems = [list componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
[listItems count];
if ([listItems objectAtIndex:0]) {
NSURL *url = [NSURL URLWithString:aux];
/*UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"hai" message:aux delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alert show];
[alert release];*/
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];
[_homeImage setImage:image];
}
else if ([listItems objectAtIndex:1])
{
NSURL *url = [NSURL URLWithString:aux];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];
[_homeImage setImage:image];
}
There is no error but I get no image. Why? How do I fix this?
If both data and image not nil, you may try to correctly initialize _homeImage.
I don'w know why, but I think what you created _homeImage like this:
_homeImage = [[UIImageView alloc] init];
Try to do something like this:
CGSize imageSize = [image size];
[_homeImage setFrame:CGRectMake([_homeImage frame].origin.x,
[_homeImage frame].origin.y,
imageSize.width, imageSize.height)];
[_homeImage setImage:image];
[image release], image = nil;

Improve speed - UIImage from NSURL

I'm using the following code to fetch some images, that are going into a tableview. But it takes ages (5-6 seconds) to get the 30 images in.
Is there a smarter/faster way to do this?
NSString *imageUrl = ......;
NSString *urlStr =
[imageUrl UrlstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *path = [[NSString alloc] initWithFormat:#"http://xxx.dk/xml.aspx%#", urlStr];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:data];
return img;
[..... release];
Here profileimg is an get from Api ,String name
NSString *profileimg=[Check objectForKey:#"IMAGE"];
imageview.image=[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:profileimg]]];
I'd rather download every new image in array of images when a cell appears on the screen and just display already saved image if a cell appears on the screen for the second time (i.e. image for this cell has been already downloaded).
use asynchronize loading of image.
check out this sample code : http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html