设置图像从我从服务器得到的tableview中? [英] setting the images in a tableview which i am getting from the server?

查看:45
本文介绍了设置图像从我从服务器得到的tableview中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的代码在表中设置从服务器加载的图像,但是我抛出了异常(并非每次都抛出),该异常与绘图错误有关

I am using the code below to set images in a table which are loaded from the server, but I have an exception thrown (not every time) which is related to a drawing error

- (void) getTheThumnbails:(NSIndexPath *)indexPath{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    GDataEntryYouTubeVideo *entry = [feedArray objectAtIndex:indexPath.row];

    NSURL *url = [[NSURL alloc] initWithString:[[[[entry mediaGroup] mediaThumbnails] objectAtIndex:0] URLString]];
    NSData *imgData = [[NSData alloc] initWithContentsOfURL:url];
    UIImage *img = [[UIImage alloc] initWithData:imgData];

    CGRect newFrame = CGRectMake(0.0, 0.0, 62.0, 62.0);

    UIGraphicsBeginImageContext(newFrame.size);

    [img drawInRect:newFrame];

    UIImage *resizedImg = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    [imgDict setObject:resizedImg forKey:[NSString stringWithFormat:@"%i",indexPath.row]];

    [[[myTableView cellForRowAtIndexPath:indexPath] imageView] setImage:resizedImg];
    [pool release];
}

请帮助我,解决我在做的错误.

Please help me folks, sorting out the mistake i am doing.

推荐答案

尝试在线程中绘制图像不是一个好习惯,请不要在线程中执行任何UI操作,它们需要在主线程中完成

Trying to draw a image in a thread is not a good practice, please do not perform any UI operations in thread, they need to be done in the main thread.

好吧,我做了上面的事情

well I did the above like

- (void) getTheThumnbails:(NSIndexPath *)indexPath{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    GDataEntryYouTubeVideo *entry = [feedArray objectAtIndex:indexPath.row];

    NSURL *url = [[NSURL alloc] initWithString:[[[[entry mediaGroup] mediaThumbnails] objectAtIndex:0] URLString]];
    NSData *imgData = [[NSData alloc] initWithContentsOfURL:url];
    UIImage *img = [[UIImage alloc] initWithData:imgData];
    [imgDict setObject:img forKey:[NSString stringWithFormat:@"%i",indexPath.row]];
    [img release];
    [self performSelectorOnMainThread:@selector(setImageInTable:) withObject:indexPath waitUntilDone:NO];
    [pool release];
}

- (void)setImageInTable:(NSIndexPath *)indexPath{

    UIImage *img = (UIImage *)[imgDict valueForKey:[NSString stringWithFormat:@"%i",indexPath.row]];

    CGRect newFrame = CGRectMake(0.0, 0.0, 62.0, 62.0);

    UIGraphicsBeginImageContext(newFrame.size);

    [img drawInRect:newFrame];

    UIImage *resizedImg = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    [imgDict removeObjectForKey:[NSString stringWithFormat:@"%i",indexPath.row]];

    [imgDict setObject:resizedImg forKey:[NSString stringWithFormat:@"%i",indexPath.row]];

    [[[myTableView cellForRowAtIndexPath:indexPath] imageView] setImage:resizedImg];

}

希望这会有所帮助.

谢谢

Madhup

这篇关于设置图像从我从服务器得到的tableview中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆