Unable to fetch data from nsurl - objective-c

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.

Related

Load url from array to ImageView in Xcode

I need load url from array (arrayCoverURL) to ImageView.
NSURL *imageURL = [NSURL URLWithString:[arrayCoverURL objectAtIndex:indexPath.item]];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:imageURL];
cell.imageView.image = [UIImage imageWithData:imageData];

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

Upload pictures to my app

I´m designing a new app for an iPad. This app will work with a barcode scanner, and when it scans a code, it will show an image asociated.
I was thinking to build that asociating de barcode to an image name for example:
- Barcode 09090909 will show 09090909.png picture
- Barcode 19191919 will show 19191919.png picture
....
I think that there is no problem with that, but the problem comes when I need to add new barcode/pictures to the app. How can I send a new picture to my App? I see that when you develop on XCode and build you App, all the data goes into de App.
Any help or clue? thanks in advance
You can simple load them on the fly from your server, but then a network connection is required. To load them async from an server you can use this code:
dispatch_queue_t image_queue = dispatch_queue_create("com.company.app.imageQueue", NULL);
dispatch_queue_t main_queue = dispatch_get_main_queue();
dispatch_async(image_queue, ^{
NSString *URLString = #"http://example.com/images/19191919.png";
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:URLString];
UIImage *image = [UIImage imageWithData:imageData];
dispatch_async(main_queue, ^{
[imageView setImage:image];
});
});
If you want you can also save the image to the cache dir (using the document dir is not recommended because it will collide with Apples iOS Data Storage Guidelines) and check before downloading an image twice, then the code will look like this:
NSString *fileName = #"19191919.png";
NSString *URLBaseString = #"http://example.com/images/";
NSString *URLString = [URLBaseString stringByAppendingString:fileName];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
NSString *dataPath = [cachePath stringByAppendingPathComponent:fileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ( [fileManager fileExistsAtPath:dataPath] )
{
UIImage *image = [UIImage imageWithContentsOfFile:dataPath];
[imageView setImage:image];
}
else
{
dispatch_async(image_queue, ^{
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:URLString];
UIImage *image = [UIImage imageWithData:imageData];
[UIImagePNGRepresentation(image) writeToFile:dataPath atomically:YES];
dispatch_async(main_queue, ^{
[imageView setImage:image];
});
});
}
This code has not been tested, but should work. Note that I'm relying on ARC to manage memory.

Problem with retrieving an image

following problem:
I created a NSXMLParser subclass to get Xbox Live profile data, the class works fine but i have a problem with displaying an image.
NSLog(#"%#", [[xbl.games objectAtIndex: i] objectForKey: #"Image64Url"]);
output:
http://tiles.xbox.com/tiles/+4/R1/1Wdsb2JhbC9ECgUAGwEfV1oiL2ljb24vMC84MDAwAAAAAAAAAPpahOQ=.jpg
displaying the image:
NSURL *url = [NSURL URLWithString:[[xbl.games objectAtIndex: i] objectForKey: #"Image64Url"]];
NSData *data = [NSData dataWithContentsOfURL:url];
gamerPicView.image = [[UIImage alloc] initWithData:data];
Does not work, but if i run:
NSURL *url = [NSURL URLWithString:#"http://tiles.xbox.com/tiles/+4/R1/1Wdsb2JhbC9ECgUAGwEfV1oiL2ljb24vMC84MDAwAAAAAAAAAPpahOQ=.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
gamerPicView.image = [[UIImage alloc] initWithData:data];
Its working, im really confused whats my mistake?
Thanks for help and greetings from switzerland.
I would print what 'url' is after your first (unsuccessful) attempt at displaying the image and see if it is the same URL or not as the code that works, since this seems like the only place something could be wrong.
Edit: And if you still can't figure out the problem, post the NSLog output for 'url' in both cases.

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