ios - download and view image from web url xcode - ios

i need download image from url and load it into UIIamge view
first i get image from URL , but data return with nil i dont know why ?
-(UIImage *) getImageFromURL:(NSString *)fileURL {
UIImage * result;
// data here return with nil
NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
result = [UIImage imageWithData:data];
return result;
}

You can use AFNetworking
And simply use: - (void)setImageWithURL:(NSURL *)url;
Or something more complex:
UIImageView image;
NSString *urlstring = [NSString stringWithFormat:#"http://url/"];
NSURL *url = [NSURL URLWithString:urlstring];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[image setImageWithURLRequest:request
placeholderImage:[UIImage imageNamed:#"placeholder.png"]
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
// Succes on loading image
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
// Fail to load image
}];

You can set your image from URL link like this:
NSString *your_url = #"http://your_link/";
image_View.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:your_url]]];
Also refer :
1) question 1
2) question 2
3) question 3

Related

How to make AFNetworking set a default image when the download fails?

I want to do the following:
When the image is loading => Must display a spinner or another image indicating loading;
When the image is loaded => Must display the image;
When the image fails => Must display a static "no image available" image.
I tried:
- (void)setImageWithURL:(NSURL *)url
placeholderImage:(UIImage *)placeholderImage
But I couldn't figure out how to handle the failure event.
Why don't you use
setImageWithURLRequest:placeholderImage:success:failure:
From the doc
And set the wanted placeholder image in the fail block?
Example:
NSURLRequest * aURLRequest = [[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString: #"A-URL"]];
UIImageView * img = [[UIImageView alloc] init];
__weak UIImageView* weakImg = img;
[img setImageWithURLRequest:aURLRequest
placeholderImage:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
//default
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
weakImg.image = [UIImage imageNamed:#"fallbackImage"];
}];

Set a downloaded image to Imageview from url - IoS

I have an url, clicking which it downloads the image. I want to set the image to the imageview. Currently have tried this but the image does not load. Can anyone suggest where i am going wrong.
Thanks in advance!
NSURL *url1 = [NSURL URLWithString:#"http://farm4.staticflickr.com/3695/9258420948_566a38bfdb_q_d.jpg"];
pic1.image = [UIImage imageWithCIImage:[CIImage imageWithContentsOfURL:url1]];
You want to use AFNetworking's category on UIImageView:
[myImageView setImageWithURLRequest:urlRequest placeholderImage:[UIImage imageNamed:#"placeholder.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
myImageView.image = image;
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"Request failed with error: %#", error);
}];
It is worth working with AFNetworking just for this call. (AFNetworking also gives you a placeholder image, and it also caches network images so subsequent calls use the cache giving you a quick response.)

UIImageView+Afnetworking doesn't Work Properly

I Have copied the contents of file from here and created a UIImageView+AFNetworking.h and imported in my implementation file
Now When I Write the following code i get this error but when i remove the block of code then everything works fine.
I want to display image in a custom table cell.The url of image i am grabbing Through JSON
NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[dictArray objectForKey:#"image"]]];
[cell.thumbnailImageView setImageWithURLRequest:imageRequest placeholderImage:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){
NSLog(#"success");
cell.thumbnailImageView.image = image;
cell.thumbnailImageView.contentMode = UIViewContentModeScaleAspectFit;
cell.thumbnailImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[cell setNeedsLayout];// To update the cell {if not using this, your image is not showing over cell.}
}failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
NSLog(#"Failure");}];
Here is the screen shot of the error
It Crashes after loading
Instead of using afnetworking You can just use dispatch method
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSString *url = [indexDic objectForKey:#"image"];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
UIImage *image = [UIImage imageWithData:imageData];
dispatch_async(dispatch_get_main_queue(), ^{
cellImg.image = image;
});
});
return cell;
}

How to display images from dropbox in UIImageView

I am using DBChooser in my application to import images from dropbox, I am getting image url like & to dasplay the image in UIImageView i have following code
UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];
imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"https://www.dropbox.com/s/7qey2let40eb9je/PRELIMINARY_FORM_2.jpg"]]];
[self.view addSubview:imageView];
but the image is not showing in application. please help me how to display the dropbox image in UIImageView, is it issue of https OR what .
See this reference: https://cantonbecker.com/etcetera/2014/how-to-directly-link-or-embed-dropbox-images/
Short answer: append raw=1 as querystring value to your image url
Let's try:
+ (void) downloadImage : (NSURL*) url withCallBack:(DownloadCallbackBlock)callback
{
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
NSHTTPURLResponse *httpresponse = (NSHTTPURLResponse *)response;
if (httpresponse.statusCode == 200)
{
callback(error,data);
}
else
{
//error
}
}];
}
And I use it:
[DownloadManager downloadImage:url withCallBack:^(NSError *error, NSData *data){
if (data)
{
UIImage *image = [[UIImage alloc]initWithData:data];
[_arrImages addObject:image];
dispatch_async(dispatch_get_main_queue(), ^{
[self processAddImage];
});
}

Asynchronous image download succeeded but image does not appear

I am writing an app that displays images from url in a view. The idea is that when the view appears, image is dowloaded and it actualizes a UIImamgeView in the view.
I am using a Asyncrounse method in this way:
-(void)downloadASyncro:(NSString*)urlLink{
NSURL * imageURL = [NSURL URLWithString:urlLink];
[self downloadImageWithURL:imageURL completionBlock:^(BOOL succeeded, UIImage *image) {
if (succeeded) {
NSLog(#"scaricaImmagineASyncro Succeded= %#",image);
picView.image = image;
}
else {
//default image
picView.image = [UIImage imageNamed:#"icon_old.jpg"];
}
}];
}
the downloadImageWithURL method is:
- (void)downloadImageWithURL:(NSURL *)url completionBlock:(void (^)(BOOL succeeded, UIImage *image))completionBlock
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if ( !error )
{
UIImage *image = [[UIImage alloc] initWithData:data];
completionBlock(YES,image);
} else{
completionBlock(NO,nil);
}
}];
}
When I call the method:
[self downloadASyncro:link];
and the operation sees end with success (NSLOG), picView.image = image; should actualize the view showing the image downloaded , should not it? But immage does not appear...
Any idea? Thanks in advance.

Resources