Loading Image from URL iOS [duplicate] - ios

This question already has answers here:
iOS: load an image from url
(9 answers)
Closed 8 years ago.
I'm trying to load an image from an image URL. All the code I have researched and tried finds a few errors, a lot having to do with new ARC compatibility.
I need the image to load into an image view.
Any help appreciated.
Thanks.

I will just adapt Jim Dovey answer from here Getting Image from URL Objective C :
Synchronous version
NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: #"http://myurl/mypic.jpg"]];
UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageWithData: imageData]];
Asynchronous version
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: #"http://myurl/mypic.jpg"]];
if ( data == nil )
return;
dispatch_async(dispatch_get_main_queue(), ^{
UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageWithData: data]];
});
});

UIImage *image = [UIImage imageWithContentsOfURL:[NSURL URLWithString:#"http://i.imgur.com/ytxO16A.png"];

Related

Show image from URL in Keyboard App Extension

I am working on keyboard app,I am trying to show image from url in the extension but code not working though this code is working fine for my iphone app
UIImageView * kanyeGifImage = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, cell.frame.size.width, cell.frame.size.height)];
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: #"some image url"]];
if ( data == nil )
return;
dispatch_async(dispatch_get_main_queue(), ^{
// WARNING: is the cell still using the same data by this point??
kanyeGifImage.image = [UIImage imageWithData: data];
tempImage = kanyeGifImage.image;
// kanyeGifImage.image = [self squareImageFromImage:cell.appImageView.image scaledToSize:320];
});
});
I'm guessing that the NSURL is not a web url, but a reference to a file in your bundle. Bundles are not by default shared between your main app and your extension. Read up on the section "Sharing Data with your Containing App" https://developer.apple.com/library/ios/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html

Is it possible to point http link(it gives an image) to the UIimageview in iOS?

I was trying to put the htttp link to the UIimageView but it doesn't show up me an image. But when i point a local file in the local storage, it shows me the image. Please help me guys.
You should do something like this
__block UIImage *img;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *url = [NSURL URLWithString:#"some_link_to_image"];
NSData *data = [NSData dataWithContentsOfURL:url];
img = [[UIImage alloc] initWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
yourImageView.image = img;
});
});
You need to download it before displaying. You have different alternatives, you could use a Cocoa API or just an external library to manage the request.
One of the external libraries to handle HTTP requests is AFNetworking
In particular it offers a very convenient category UIImageView+AFNetworking.h You can use it like this:
#import "UIImageView+AFNetworking.h"
//...
NSURL *url = [NSURL URLWithString:#"http://here_your_url_to_the_image"];
[yourImageView setImageWithURL:url];

Simple For loop issue

I have a two for loops in my Xcode project which are using to change the image in 9 UIImageViews. The images in these UIImageViews is downloaded from a server and then presented.
My for loops use 3 different integers to figure out what image to show:
next, current_photo and previous are integers.
I have two UIButtons which control showing the next and previous set of images.
When I want to show the next set of images, I made and use the following for loop:
NSArray *imageViews = #[picview_1, picview_2, picview_3, picview_4, picview_5, picview_6, picview_7, picview_8, picview_9];
for (next = current_photo; next < (current_photo+9); next++) {
NSString *next_set = [NSString stringWithFormat:#"%#%i.%#", SERVER_URL, next+1, FORMAT_TYPE];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: next_set]];
UIImage *image = [[UIImage alloc] initWithData:imageData];
[[imageViews objectAtIndex:(next-9)] setImage:image];
}
current_photo = next;
This for loop works perfectly and all 9 UIImagesViews change image.
However, when I want to show the previous set of images in the 9 UIImageViews, the following for loop does NOT work properly for some reason:
NSArray *imageViews = #[picview_1, picview_2, picview_3, picview_4, picview_5, picview_6, picview_7, picview_8, picview_9];
for (previous = current_photo; previous > (previous-9); previous--) {
NSString *next_set = [NSString stringWithFormat:#"%#%i.%#", SERVER_URL, previous+1, FORMAT_TYPE];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: next_set]];
UIImage *image = [[UIImage alloc] initWithData:imageData];
[[imageViews objectAtIndex:(previous-previous)] setImage:image];
}
current_photo = previous;
What is wrong with my for loop? Please explain.
Below is what happens when my app is opened:
Below is what happens when the next button is pressed:
And finally when pressing the back button the app just freezes.....
Why? Whats wrong? Please help.
Thanks for your time :)
For one thing:
[[imageViews objectAtIndex:(previous-previous)] setImage:image];
will always be setting the image at [0] and none of the others will have anything set for them.
Also
(previous = current_photo; previous > (previous-9); previous--)
will loop forever! Every time you do a previous--, the thing you are comparing against to know when to stop, previous-9 also goes down one.
I would recommend this:
for (previous = current_photo; previous > (current_photo-9); previous--) {
NSString *next_set = [NSString stringWithFormat:#"%#%i.%#", SERVER_URL, previous+1, FORMAT_TYPE];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: next_set]];
UIImage *image = [[UIImage alloc] initWithData:imageData];
[[imageViews objectAtIndex:(8 - (current_photo - previous))] setImage:image];
}

app slowing down due to image load issue

I am having problem with the loading of content in the app, I see that the data is fetched by the app but the images take lot of time to load, is there any possibility to load images afterwords. The code is below:
NSDictionary *dict=[discussionArray objectAtIndex:indexPath.row];
UIImageView *avatarimage = (UIImageView *)[cell viewWithTag:4];
NSString *photoStrn=[dict objectForKey:#"photo"];
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSString *u=[NSString stringWithFormat:#"http://%#",photoStrn];
NSURL *imageURL=[NSURL URLWithString:u];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
dispatch_sync(dispatch_get_main_queue(), ^{
UIImage *dpImage = [UIImage imageWithData:imageData];
if (dpImage==nil)
{
dpImage = [UIImage imageNamed:#"profileImage.png"];
}
avatarimage.image = dpImage;
});
If you want more details I will provide :)
You can use GCD for doing this:
dispatch_async(dispatch_get_global_queue(0,0), ^{
for (NSDictionary *dic in discussionArray)
{
NSString *photoStr=[dic objectForKey:#"photo"];
NSString * photoString=[NSString stringWithFormat:#"http://%#",photoStr];
UIImage *dpImage = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:photoString]]];
if (dpImage==nil)
{
dpImage = [UIImage imageNamed:#"profileImage.png"];
}
}
});
Get SDWebImage Here and add that in your project and include
UIImageView+WebCache.h
in class implementation file
UIImageView *imag=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
[imag setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",[[jsonarray objectAtIndex:indexPath.row]valueForKey:#"imgurl"]]] placeholderImage:[UIImage imageNamed:#"Icon#2x.png"]];
[self.view addSubview:imag];
[imag release];
SDWebImage will be more useful for loading images from URL.
Hope this Helps !!!
James,
With this line,
[NSData dataWithContentsOfURL:[NSURL URLWithString:photoString]]]
you make a synchronous network call on the main thread. The current thread will hang untile the network call is complete.
The solution would be to do an asynchronous network call. The AFNetworking library provides a great category to load images asynchronously : UIImageView+AFNetworking.

"No visible #interface declares the selector" error

I inserted an image view in my view controller but dont actually know how to display image on it.
I tried this:
- (void)viewDidLoad
{
[super viewDidLoad];
id path = #"http://thestylishdog.com/wp-content/uploads/2009/07/cute-dog2.jpg";
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImageView *firstImage = [[UIImage alloc] initWithData:data cache:NO];
}
But then it says that 'No visible #interface for UIimage declares the selector initwithData:cache'
I tried adding this line to the .h file but does not work either:
#property (nonatomic, retain) UIImageView *firstImage;
I know that the last step would be to assign that .h create class to the UIimage in the viewcontroller.
You can use try this code to add image from the path in your imageview
NSURL *url = [NSURL URLWithString:#"http://thestylishdog.com/wp-content/uploads/2009/07/cute-dog2.jpg"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
imageView = [[UIImageView alloc] initWithImage:image];
Don't forget to show your imageView as outlet to xib if you are adding through GUI otherwise add it to your view as subview
Try [[UIImage alloc] initWithData:data]; you can cache using an NSCachedURLResponse for the response.
You can use this to load images from URLs:
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://..."]]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
But this will block your app until your image loads. It's best to load your images asynchronously when using images from the web. If the images are downloaded in a different thread user can keep interacting with the app. You can check this example from apple about lazy loading images: LazyTableImages
Or you can checkout this tutorial for async image download:
UItables with downloaded images

Resources