开发者

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 ];
    开发者_JS百科//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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜