Syntax for combining NSURL and normal string? - objective-c

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..

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.

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

Objective C, NSData never gets data from web contents

NSData *imgData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:post.imageURL]];
UIImage *img = [[UIImage alloc] initWithData:imgData];
I have this code and imgData always gets nil. I checked post.imageURL and it returns a string which in a url format. I also replaced the string with a constant string(URL format) that I hardcoded but imgData still got null. This code runs on the background. Do you guys see any problem?
Your code looks good, are you sure your URL is valid ?
If post.imageURL is not valid, thus URLWithString: will be null and also your data.
I have tried with "http://www.google.com/intl/en_ALL/images/srpr/logo1w.png" to be sure, and it worked, so I guess the problem come from your URL.
Split your code :
NSURL *url = [NSURL URLWithString:#"http://www.google.com/intl/en_ALL/images/srpr/logo1w.png"];
NSData *imgData = [[NSData alloc] initWithContentsOfURL:url];
UIImage *img = [[UIImage alloc] initWithData:imgData];
And check the value of each of the variable.

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

Having trouble with this NSURL method

Can anybody tell me excatly how I should execute this method on an NSURL object?
URLByResolvingSymlinksInPath
Compiler says that the method can't be found when I execute it.
Thanks a lot.
This is where I am trying to implement the code:
- (void)data
{
NSURL *url = [[NSURL alloc] initWithString: [[user data] objectAtIndex: 5]];
NSURL * url2 = [[NSURL alloc] init];
url2 = [url URLByResolvingSymlinksInPath];
NSData *newImage = [[NSData alloc] initWithContentsOfURL:url2];
NSImage *image = [[NSImage alloc] initWithData:newImage];
imageView.layer.masksToBounds = YES;
imageView.layer.cornerRadius = 3.0;
[imageView setImage:image];
[user autorelease];
}
Assuming that you have NSURL named something like myURLWithSymlinks, you would do something like:
NSURL * myURLWithoutSymlinks = [myURLWithSymlinks URLByResolvingSymlinksInPath];
Note that according to the docs, URLByResolvingSymlinksInPath is only available in iOS4.0 or above and MacOSX 10.6. If you are using an older version of iOS (or MacOSX), that could be the cause of your problem.