2012-03-26 56 views
0

我尝试从UITableViewCell中异步加载服务器的UIImages。 我的代码在模拟器中工作正常,但不在设备上。我的代码如下,UIImage无法正常工作?

- (UITableViewCell *)tableView:(UITableView *)tableView 
    cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
IScopeCustomTableCell *cell = (IScopeCustomTableCell *)[tableView dequeueReusableCellWithIdentifier:CellClassName]; 

if (!cell){ 
    NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil]; 
    cell = [topLevelItems objectAtIndex:0]; 
} 

cell.delegate = self; 
cell.videoTitle.text = [[videoDataArray objectAtIndex:indexPath.row] objectForKey:@"VideoTitle"]; 
cell.videoLink = [[videoDataArray objectAtIndex:indexPath.row] objectForKey:@"VideoLink"]; 
cell.videoThumbnailImageLink = [[videoDataArray objectAtIndex:indexPath.row] objectForKey:@"VideoThumbnail"]; 
cell.videoThumbnail.tag = indexPath.row; 
cell.tag = indexPath.row; 
[cell.activityIndicator startAnimating]; 
NSInvocationOperation *operation = [[NSInvocationOperation alloc] 
            initWithTarget:self 
            selector:@selector(loadImage:) 
            object:cell]; 
[queue addOperation:operation]; 
return cell; 
} 

- (void)loadImage:(IScopeCustomTableCell *)cell { 
NSLog(@"Image link :- %@", cell.videoThumbnailImageLink); 

//NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:cell.videoThumbnailImageLink]]; 
NSData* imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:cell.videoThumbnailImageLink]]; 
UIImage* image = [UIImage imageWithData:imageData]; 

cell.videoThumbnail.image = image; 
[cell.activityIndicator stopAnimating]; 
[cell.activityIndicator removeFromSuperview]; 

//[self performSelectorOnMainThread:@selector(displayImage:) withObject:image waitUntilDone:NO]; 
} 

上模拟器上述代码细但不能在设备,becoz,的UIImage *图像获取即使含有适当的数据的NSData的LoadImage方法(0X0)空。

+0

文件大小的howmany MB来自服务器??? – parag 2012-03-26 07:00:33

+0

@paragkavar,20KB – Tirth 2012-03-26 07:22:50

+0

对不起,明显的,但你已经检查你的设备可以访问网络,不是吗?另外,每次单元显示时发射一个线程并不是最好的解决方案。您可以在Web上找到许多异步加载图像的教程。 – Leonardo 2012-03-26 07:31:47

回答

0

此链接包含我法师缓存代码,它的工作正确,我想。

https://github.com/jakemarsh/JMImageCache

+0

http://maniacdev.com/2012/04/best-resources-in-ios-development- 2012年4月2日/ utm_source = feedburner&utm_medium = feed&utm_campaign = Feed%3A + maniacdev +%28iPhone%2C + iOS + 4%2C + iPad + SDK + Development + Tutorial + and + Programming + Tips%29 – Tirth 2012-04-02 06:46:06

+0

http:// gameit .RO /标签/ cocos2d的-X / – Tirth 2012-04-17 08:13:28

0

你在做什么是不该做,因为它会创造出很多自动释放的对象,并会增加您的应用程序的大小,最好的办法,你也不会释放你的operation ...所以首先后释放你的operation[queue addOperation:operation];

,并使用此代码,而不是用于获取图像数据,并将其存储在你的形象......

NSData* data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:cell.videoThumbnailImageLink]]]; 
UIImage* img = [[UIImage alloc] initWithData:data]; 
[data release]; 
cell.videoThumbnail.image = image; 
[img release]; 

希望这排序您的问题..

+0

我的朋友,我已经试过这个,但没有为我工作...... – Tirth 2012-03-26 07:23:45