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;
}
Related
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"];
}];
I have got a productImageArray that contains url as elements of the array.
I'm trying to load those urls in my image view.
Following is the way as of how I'm loading it.
UIActivityIndicatorView *spinner=[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.center=CGPointMake(160.0,240.0 );
spinner.hidesWhenStopped=YES;
[self.view addSubview:spinner];
[spinner startAnimating];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
dispatch_async(queue, ^{
NSData *storeImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:productImageArray[indexPath.row]]];
self.productImage.image = [UIImage imageWithData:storeImageData];
dispatch_sync(dispatch_get_main_queue(), ^{
[spinner stopAnimating];
});
});
The problem is that,
Only the last cell of my tableview loads the image whereas the remaining cell does not load the image from the url
Is there any other better way of loading the image from url directly into my UIImage using native methods?
When I use the following code, each cell of my tableview loads the image data but still it freezes the User interface till the data is loaded completely
NSData *storeImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:productImageArray[indexPath.row]]];
self.productImage.image = [UIImage imageWithData:storeImageData];
#Anbu.Karthik answer is right.
But, maybe the simplest solution is to use something like SDWebImage no? This library will handle this issue and much more (cache, error management, proper tableview cells handling, ...).
I think you should, at least, take a few minutes to look at it: https://github.com/rs/SDWebImage
Edit:
If you use SDWebImage, and UIActivityIndicator-for-SDWebImage, you can replace your entire code by this:
[self.productImage setImageWithURL:[NSURL URLWithString:productImageArray[indexPath.row]]
usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
More informations on UIActivityIndicator-for-SDWebImage: https://github.com/JJSaccolo/UIActivityIndicator-for-SDWebImage
try this
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul);
dispatch_async(queue, ^{
NSData *storeImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:productImageArray[indexPath.row]]];
dispatch_sync(dispatch_get_main_queue(), ^{
[spinner stopAnimating];
self.productImage.image = [UIImage imageWithData:storeImageData];
});
});
or try like
self.productImage.image = nil; //// [UIImage imageNamed:#"default.png"];
NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:productImageArray[indexPath.row]] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (data) {
UIImage *currentImage = [UIImage imageWithData:data];
if (currentImage) {
dispatch_async(dispatch_get_main_queue(), ^{
UITableviewCell *getCurrentCell = (id)[tableView cellForRowAtIndexPath:indexPath];
if (getCurrentCell)
self.productImage.image = currentImage;
});
}
}
}];
[task resume];
NSString *url_Img1 = #"Your url";
Uiimageview *view_Image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url_Img1]]];
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
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];
});
}
I am trying to set image in UICollectionViewCell, via collectionView:cellForItemAtIndexPath:. I am using AFNetworking Image Category UIImageView+AFNetworking.h method for loading image from cloud server.
The problem is that, I am always hitting failure block. When open the link in browser window, I can see the image.
Here is the sample code:
__weak UICollectionViewCell *blockcell = cell;
NSURL *imageUrl = [NSURL URLWithString:#"https://ec2-75-101-163-253.compute-1.amazonaws.com/static/icons/app-excel%402x.png"];
[cell.appImage setImageWithURLRequest:[NSURLRequest requestWithURL:imageUrl] placeholderImage:[UIImage imageNamed:#"file-gray.png"] success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
blockcell.appImage.image = image;
[blockcell setNeedsLayout];
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(#"fail for %#",request.URL);
}];
There might be a simple/silly mistake. Can anyone help me.
Thanks in advance.