I have a method that I'm calling from cell in the viewForRowAtIndexPath method.
The method containts only this code
NSURL *imageURL = [NSURL URLWithString:urlString];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
dispatch_async(dispatch_get_main_queue(), ^(void){
completionBLock(image);
});
Images are loading very slowly and sometimes not at all. Everytime the cell goes back into view the method gets called again.
This is in my cellForIndexPathRow
[Method getImage:brochureModel.brochureCoverUrl Oncompletion:^(UIImage *image) {
cell.imageView.image = image;
}];
How can I download images from a url and display it in a collection view reusable cells.
Note: I'm trying to not use any third party libraries for my app
My problem is with the caching of the images. I tried this with self.imageCache being a mutableDictionary.
if (self.imageCache[urlString]) {
UIImage *imageFromCache = self.imageCache[urlString];
onImageDidLoad(imageFromCache);
} else {
[API fetchImageFromUrl:urlString onDidLoad:^(UIImage *image) {
self.imageCache[urlString] = image;
onImageDidLoad(image);
}];
}
Related
NSString *img=[arrImages objectAtIndex:i];
imgScrollView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:img]]];
I have displayed my code. The URL links have been stored into an array and the images are also fetching into uiimage view, but only the first two images are loaded into the imageview while the others didn't load .
arrImages:
inside image viewhttp://localhost:8888/welcome/company/0/images/TV Repair.png
this is the link present in the arrImages i don't know why first two images has been loading.
FOR LOOP:
for(NSDictionary *DicHoleCategories in ArrCategory)
{
StrName=[DicHoleCategories objectForKey:#"image"];
if(StrName!=nil)
{
subimages=[NSString stringWithFormat:LocalImage"%#",StrName];
[DicAllValues setObject:subimages forKey:#"image"];
NSError *error =nil;
}
[arrImages addObject:[DicAllValues objectForKey:#"image"]];
}
Can anyone find the issue and help me please?
Go through the below coding
//url string
NSString *ImageURL = #"yoururl";
//string to data conversion
NSData *imagedata = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
//set image
imageView.image = [UIImage imageWithData:imageData];
please follow the below code
if(StrName!=nil)
{
subimages=[NSString stringWithFormat:LocalImage"%#",StrName];
[DicAllValues setObject:subimages forKey:#"image"];
NSError *error =nil;
[arrImages addObject:[DicAllValues objectForKey:#"image"]];//add here!
}
Plz check now and tell me if its working or not!
I want to get thumbnail image or blank image first while app is loading image data from parse database. After finish loading, the image view will display the image I load from parse.
So far, I have following codes
PFFile *thumbnail = object1[#"PostFiles"];
NSData *imageData = [thumbnail getData];
UIImage *image = [UIImage imageWithData: imageData];
I use PFtableviewcontroller. when I scroll down, my code will load image, the controller can't move smoothly.
The issue is that you are running a synchronous call for a task that could take a varying amount of time. Try getting the NSData using InBackgroundWithBlock:
image = nil; //prevent images from being shown erroneously
[thumbnail getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (data && !error) {
image = [UIImage imageWithData:data];
} else {
//maybe set a default image here if there is none?
}
}];
I'm working with a UICollectionView and I need to load images in to the collection cell.
Here is the code
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSURL *imageURL = [NSURL URLWithString:post.cover_img_url];
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
[cell.cover_img setImage:image];
});
});
post.cover_img_url = the_url_for_the_image;
cell.cover_img = the_UIImageView_object;
When I set the image not from the web, it works perfectly.
But when I try from the web, the UIImageView is blank not image.
Anyone have any ideas?
Your code looks like it should be working, but there are a couple of things that you need to test. First, you need to check to make sure that imageURL is both non-nil, and points to exactly where you think it's pointing. Copy paste a log result to your desktop browser if you have to.
NSLog(#"%#",imageURL);
Second, in the event that fetching the image fails, you need to be able to detect this. I recommend that if you continue to use the dataWithContentsOfURL: route that you at least use the following to check the error:
NSError *error = NULL;
NSData *data = [NSData dataWithContentsOfURL:imageURL options:NSDataReadingMappedIfSafe error:&error];
if (error) {
// ...
}
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.
NSURL *url = [NSURL URLWithString:
pictureUrl];
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
NSLog(#"image width:%#",image.size.width);
I am getting null from this log? The image is valid and displaying correctly?.
This is in a table cell
Try this:
NSLog(#"image width:%f",image.size.width);
Besides that, I hope you are calling this on a background thread (not within cellForRow), otherwise loading your cells will be very slow and screw up user interaction.