Populating Data from NSMutableArray in iPhone - iphone

I have three arrays and I have copied all these arrays into a single array. All these three arrays are arrays of dictionaries.All arrays has a field called picture, but that pictures is coming from different sources- URL in one array, data in other and files in the third one.
Say, Array1 has dictionaries with a key - picture and its loaded from NSURL.
Similarly, Array2 and Array3 has dictionaries with same key name - picture and loaded from ContentofFiles and NSData.
Now, I want to populate tableview, of course,m having Custom UITableViewCell, it has image view as its content view. To load that image, what should I do.
I was doing this thing..
NSURL *url = [NSURL URLWithString:[[contactList objectAtIndex:indexPath.row] objectForKey:#"picture"]];
cell.contactImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
But, this will crash if cell.contactImageView.image don’t receive image from NSURL.So, what should I do? Any help, will be appreciated
But,

all you is to check if the received image is null and if it is, then set a template photo image called no photo like a photo on facebook when no profile picture is selected
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
if (img)
cell.contactImageView.image = img;
else
cell.contactImageView.image = [UIImage imageNamed:#"no_photo.png"];

If these images are retrieved from the net, i would suggest not using [NSData dataWithContentsOfURL:].
You should be using a non blocking method, that loads images asynchronously. Using this method on numerous rows in a table will cause performance issues.
Here's my recommendation , use the SDWebImage Library . Easy to use, and even easier to install.
Once you add the library to your project, simply #import the UIImageView+WebCache.h class usage example is below.
[cell.contactImageView.image setImageWithURL:[NSURL URLWithString:[[contactList objectAtIndex:indexPath.row] objectForKey:#"picture"]]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];

You can add one more key in dictionary which will specify from where to take photo and accordingly you can provide image to cell.contactImageView, if you want to provide image from different sources.

Thanks a lot buddies for your Quick reply,especially #skram, due to your suggestion,my tableview performance has increased much. the Question, that I have asked,the better answer that I have thought of is using iskindofClass. If image is coming from any class, on a condition ,we can check from where that image was coming and populate our image accordingly.
if ([[[contactList objectAtIndex:indexPath.row] objectForKey:#"picture"] isKindOfClass:[UIImage class]])
{
cell.contactImageView.image = [[contactList objectAtIndex:indexPath.row] objectForKey:#"picture"];
}
else if ([[[contactList objectAtIndex:indexPath.row] objectForKey:#"picture"] isKindOfClass:[NSString class]])
{
cell.contactImageView.image = [[contactList objectAtIndex:indexPath.row] objectForKey:#"picture"];
}
else if([[[contactList objectAtIndex:indexPath.row] objectForKey:#"picture"] isKindOfClass:[NSURL class]])
{
[cell.contactImageView setImageWithURL:[NSURL URLWithString:[[contactList objectAtIndex:indexPath.row] objectForKey:#"picture"]]placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
}
Now, I’m able to populate the tableview properly.Once again thanx to scram.

Related

how to show images at cell.imageview.image from NSArray?

Image Array=[NSArray arrayWithObjects:[UIImage imageNamed:#"1 (1).gif"],
[UIImage imageNamed:#"1 (2).gif"],[UIImage imageNamed:#"1 (3).gif"],[UIImage imageNamed:#"1 (4).gif"],[UIImage imageNamed:#"1 (5).gif"],[UIImage imageNamed:#"1 (6).gif"],[UIImage imageNamed:#"1 (7).gif"],nil];
//here is another array of objects showing in table view
alphabetsArray =[[NSMutableArray alloc]initWithObjects:#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z",nil];
//everything working fine until when i add this code,app crashes with SIGABRT
cell.imageView.image=[UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];
Actually i need to ask some more things:
Is there any problem if number of images in array isn't equal to number of object in tableview?
Is there any other way to add images if they are huge in number except of uiimage imageNamed:"whatever.jpeg"
What if add a folder and give its address or name and it picks up images by itself..
What is most reasonable method of adding images in project if they are 250 in number(i-e sqlite,coreData,hardcode in project as i did)?
First, there is issue with this line [UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]].
The [imageArray objectAtIndex:indexPath.row] is returning UIImage object in your current implementation, and you are trying to get UIImage with name of UIImage object.
You should just return the UIImage object directly to the cell.imageView.image.
cell.imageView.image = [imageArray objectAtIndex:indexPath.row];
Second, I am not sure how many rows are there in the table, but the method objectAtIndex can throw exception if the index is more than the number of elements in the array, causing app crash.
1) There would be no problem with this, unless you want to have the same number. It won't throw an error, but your app logic will not work properly.
2) Not that i know of, It's easy to do this though, use an easy naming convention such as image01, That way you can just copy and paste the code and change the ending numbers of the image
3) Im not sure of what you are asking.
4) Typically you would just need to add the images to your project via drag-N'-drop or by picking the files. Core data and sqlite are for the actual app that you are developing, not to store images in Xcode.
As for the main question, You say you want to show the images from an NSArray, if thats the case, just get the index path of the tableview, and look up the corresponding image using objectAtIndex:
Hope that helps a bit :)
Actually i have figured something out , there is an easy way to do.
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#.gif", [TextShownInTableArray objectAtIndex:indexPath.row]]];
i have alphabetics in this array, and i wanted to use different images with each of the alphabet, so just added images in project with same name as alphabets had. it worked fine with this line of code. TextShownInTableArray is your array with text :)
Image Array=[NSArray alloc]initWithObjects:[UIImage imageNamed:#"8.gif"],
[UIImage imageNamed:#"2.gif"],[UIImage imageNamed:#"3.gif"],[UIImage imageNamed:#"6.gif"],[UIImage imageNamed:#"5.gif"],[UIImage imageNamed:#"4.gif"],[UIImage imageNamed:#"7.gif"],nil];
your array alloc and that array 'retain' this array and try this code .thanks
[imageArray retain];
only change in your code this line . and solve your problem
cell.imageView.image=[imageArray objectAtIndex:indexPath.row];
i hope this code useful for You . Check this
cell.imageView.frame = CGRectMake(5, 5, 50, 50);
NSString *imgstr = [[Arr objectAtIndex:indexPath.row] objectForKey:#"thumb"];
cell.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgstr]]];

What condition is set in uitableview lazy loading to load images

I fetched images from json and put all image url's in array, rather than calling json again. Now i don't know how to set the condition that it load images as table scrolled. Here is the method i called for this.
+ (NSMutableArray *) createImg: (NSArray*)sampleData
{
NSMutableArray *arrImg = [[NSMutableArray alloc]init];
for(int i=1; i<=[sampleData count]; i++)
{
NSString *strOfUrl = [sampleData objectAtIndex:i];
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:strOfUrl]]];
if(img == NULL)
{
NSLog(#"null no image");
}
else{
[arrImg addObject:img];
}
}
return arrImg;
}
Please guide for the above and feel free to ask anything if not clear in this.
Thanks in advance.
I know this might not be exactly what you are looking for. However, I'd suggest you to use this AsyncImageView. It'll do all the logic you need for lazy loading. Also, it'll cache the images. To call this API:
ASyncImage *img_EventImag = alloc with frame;
NSURL *url = yourPhotoPath;
[img_EventImage loadImageFromURL:photoPath];
[self.view addSubView:img_EventImage]; // In your case you'll add in your TableViewCell.
It's same as using UIImageView. Easy and it does most of the things for you. AsyncImageView includes both a simple category on UIImageView for loading and displaying images asynchronously on iOS so that they do not lock up the UI, and a UIImageView subclass for more advanced features. AsyncImageView works with URLs so it can be used with either local or remote files.
Loaded/downloaded images are cached in memory and are automatically cleaned up in the event of a memory warning. The AsyncImageView operates independently of the UIImage cache, but by default any images located in the root of the application bundle will be stored in the UIImage cache instead, avoiding any duplication of cached images.
The library can also be used to load and cache images independently of a UIImageView as it provides direct access to the underlying loading and caching classes.
use this. It is perfect way to show images while scrolling:
https://github.com/enormego/EGOImageLoading

How can i store photos if they are downloaded?

I have a UITableViewCell that contains 4 photos and i get these photos from the web but the problem is when i scroll down the UITableView these photos are downloaded again
And this is the code:
ITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cellPhoto"];
if (cell == nil) {
NSArray *nibObject = [[NSBundle mainBundle] loadNibNamed:#"CustomCellThumbnails" owner:self options:nil];
cell = [nibObject objectAtIndex:0];
}
// Get the photos
int getPhotos = indexPath.row * 4;
for (int i = 1; i <= 4; i++) {
if (getPhotos < [imageArray count])
{
UIButton *imageButton = (UIButton*)[cell viewWithTag:i];
NSString *url = [imageArray objectAtIndex:getPhotos];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.siteweb.com%#",url]]];
UIImage *imageFieldProfile = [[UIImage alloc] initWithData:imageData];
dispatch_async(dispatch_get_main_queue(), ^{
// Set the phoyo to the UIButton
[imageButton setBackgroundImage:imageFieldProfile forState:UIControlStateNormal];
[imageFieldProfile release];
// Set the corner of UIButton
[imageButton.layer setCornerRadius:5.0];
[imageButton.layer setMasksToBounds:YES];
imageButton.tag = getPhotos;
});
});
[imageButton addTarget:self action:#selector(displayPhoto:) forControlEvents:UIControlEventTouchUpInside];
}
getPhotos ++;
}
You should use the view controller to fill the cells, not the UITableViewCell. If you do that, it's not only a better coding style, it's also easier to save the data.
Anyway, if you really must: initialize the UITableViewCell with some kind of storage table, so that you can store the data you downloaded: rewrite initWithStyle:reuseIdentifier: to initWithStyle:reuseIdentifier:usingCacheTable:
But, again, the correct way to do this is to load the data in the view controller and let the UIView subclasses simply only show stuff.
You should save or cache the images in another object when they are downloaded, not in the table view cell. Perhaps using some sort of data source or model object from which the table view cell requests the images, instead of having the table view cell directly make any URL requests. Then the model object can cache images after downloading and before handing them to the cell.
You could use a combination of lazy loading image views in combination with local caching. This would be rather easy to accomplish using ASIHTTPRequest in combination with correctly setup caching flags.
ASIHTTPRequest is extremely easy to use and its caching is very well controllable.
In contrast to the other solutions suggested, I would stick to use UITableView and its UITableViewCells as this will allow you to use de/queued cells without having to build such logic yourself.
I have used such solution for a major newsmagazine app (over 2mio downloads) and am totally satisfied with the results.

How to improve performance while using UIWebView in cells of table view?

I am creating an application which uses a web service.And retrieves a list of users and there details like images, user id and there names.I displayed all the information related to users in table view, Thus each of the cell in the table view has an image with the for tables in it. I am using a Custom cell class to create the individual cells. selecting any row in table presents a detail view (using navigation) which show a larger image and all the details related to the particular user i selected.
in customcell and detail view class i am using web view to display image.
But when i run the app the images gets a bit of delay to display and same happens with the detail view.
Is there any alternative so that i can improve the performance of the table view so that i can have smooth scrolling of table view with out any delay in image loading and detail view??
here is the code...
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CustomCellIdentifier = #"CustomCellIdentifier ";
CustomCell *cell = (CustomCell *)[tableView
dequeueReusableCellWithIdentifier: CustomCellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomCell" owner:self options:nil ];
//cell = [[CustomCell alloc] initwi ];
for (id oneObject in nib)
if ([oneObject isKindOfClass:[CustomCell class]])
cell = (CustomCell *)oneObject;
}
NSUInteger row = [indexPath row];
NSString *imageName = [imgArray objectAtIndex:row];
NSString *completeImageUrl = [[NSString alloc] initWithFormat:#"http://122.160.153.166:201/%#", imageName];
NSURL *url = [NSURL URLWithString:completeImageUrl];
//NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:imageData]; /// check to see if we are getting all the arrays such as image array and userId array and name array of same size..
if(image == nil)
{
}
cell.imgView.image = image; /// other wise an execption of out out array range will be shown
[image release];
[imageName release];
//[cell.webView initWithFrame:CGRectZero];
//[cell.webView loadRequest:requestObj];
//cell.webView.frame = CGRectMake(20, 0, 80.0, 64);
cell.userIdLabel.text = (NSString *)[userId objectAtIndex:row];
cell.nameLabel.text = (NSString *)[userName objectAtIndex:row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}`
i think problem can be in imgArray array as i am setting it from other class. Where i request the web service and fetched all the data about users .
Any help is greatly appreciated . Thanks in advance
I got the same problem. For that I used EGOImageCache and some thing.
Find this Url for EGOImage
Download files those names starts with "EGO", and add those to your project.
and write the following code where ever you want to put the image:
EGOImageView *imgView = [[EGOImageView alloc] initWithFrame:CGRectMake(50,50,220,360)];
NSString *imageName = [imgArray objectAtIndex:row];
NSString *completeImageUrl = [[NSString alloc] initWithFormat:#"http://122.160.153.166:201/%#", imageName];
NSURL *url = [NSURL URLWithString:completeImageUrl];
imgView.imageUrl = url;
[self.view addSubView:imgView];
Thats it. Use the similar code, where ever you want the image.
At the first time while loading it will take some time to download the image. But later it will cache the image and use it the next time.
Is there any alternative so that i can improve the performance of the table view so that i can have smooth scrolling of table view with out any delay in image loading
For this the answer is Lazy Loading of images in cells
You can implement lazy loading on webviewDidLoad which shows that webview has loaded completely
Using a UIWebView to display a single image is overkill. You should be using NSURLConnection (or one of many alternative HTTP wrappers/libraries) to load the image data and UIImageView to display it in each of your table cells. In my experience, there is no way (or at least no straightforward way) to eliminate the rendering delay when using UIWebView.
Why are you using the webview for displaying the images. Imageview should be used instead. If you are getting the images from the server then you should get those in separate thread and after you receive the images you should reload that particular row.
Some code snippet is like this:
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
...
...
UIImage *img = (UIImage *)[imagesArray objectAtIndex:indexPath.row];
if(img == nil){
[imgView setImage:temporaryImage];
[NSThread detachNewThreadSelector:#selector(getTheThumnbails:) toTarget:self withObject:indexPath];
}
else
[imgView setImage:img];
for further assistance look at this
Hope this helps.
Thanks,
Madhup
okay finally i removed the uiweb view from my app and used Imaged view and succeed.
i used the link that was given by the developer of the tweetie . here it is link text
i used the ABTableViewCell class and created the whole cell using the code.
The scrolling was very jerky when i executed the app. after applying almost 4 hours and with the help of lazy loading i was able to run my app smoothly. I stored the data which is retrieved from url into an array then applied the concept in tableView:cellForRowAtIndexPath:
method..
Thanks you guys for helping me ...
Three20 has a rich-text table view cell class (TTStyledTextTableCell I think) - should render significantly faster than an embedded UIWebView. You could also roll your own, though that would take a lot longer.

Access an array and get the stringvalue of an element

i would like to know how i can access the name of an image when the syntax is something like that:
NSArray* imgArray =[[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"test_01.png"],
[UIImage imageNamed:#"test_02.png"],
[UIImage imageNamed:#"test_02.png"], nil];
If i print out a specific index, i get the address.
NSLog(#"TEST: %#", [imgArray objectAtIndex:1]);
Output:
TEST: <UIImage: 0x4b12cd0>
How do i get the stringvalue?
Any ideas?
Thanks for your time.
UIImage does not expose the name of file it was initialized with. There is no way to access it from the image object.
You should keep track of the string values yourself. The UIImage object is somewhat closed with respect to the data backing the image.
You could of course subclass UIImage and implement
+(UIImage*)imageNamed:(NSString*)name
{
self.keepName = name;
return [super imageNamed:name];
}
adding a keepName string property, but I wouldn't see why you would.
If you want to store & retrive image from NSMutableArray than you have to take your image in UIimageView and than store it in NSMutableArray.. and from particular index you can retrieve it back.
K.D