I'm using the three20 project for my iPhone app. I've narrowed my problem down and I'm now just trying to re-create the 'Web Images in Table' example that comes with the project. I've copied the code exactly as in the project, with the exception that I do not use the TTNavigator (which the example does) but I am adding my TTTableViewController manually to a tabBar.
我正在为我的iPhone应用程序使用three20项目。我已经缩小了我的问题,现在我只是想重新创建项目附带的'Web Images in Table'示例。我完全按照项目中的方式复制了代码,但我没有使用TTNavigator(示例),但我手动将TTTableViewController添加到tabBar。
The problem is as follows; the images in the table should load automatically from the web, like in the example. But they only load after I scroll the table up and down.
问题如下;表中的图像应自动从Web加载,如示例中所示。但它们只是在我上下滚动表格后加载。
In the console it clearly says it is downloading the images, and you see the activity indicator spinning like forever.. And unless I scroll up and down once, the images will never appear.
在控制台中,它清楚地说它正在下载图像,你会看到活动指示器像永远一样旋转。除非我上下滚动一次,否则图像将永远不会出现。
Anyone? Thanks in advance.
任何人?提前致谢。
P.S: If I'm using this code in any random UIView, It also doesn't work (only shows a black square):
P.S:如果我在任何随机UIView中使用此代码,它也不起作用(仅显示黑色方块):
TTImageView* imageView = [[[TTImageView alloc] initWithFrame:CGRectMake(30, 30, 100, 100)] autorelease];
imageView.autoresizesToImage = YES;
imageView.URL = @"http://webpimp.nl/logo.png";
[self.view addSubview:imageView];
If I put this code in my AppDelegate (right onto the window), it does work .. strange?
如果我把这个代码放在我的AppDelegate中(直接放到窗口上),它确实有用......奇怪吗?
POSSIBLE SOLUTION: Although I stopped using TTImageView for this purpose, I do think I found out what the problem was; threading (hence accepting the answer of Deniz Mert Edincik). If I started the asynchronous download (because basically that is all the TTImageView is, an asynchronous download) from anywhere BUT the main thread, it would not start. If I started the download on the main thread, it would start immediately..
可能的解决方案:虽然我为此目的停止使用TTImageView,但我确实认为我发现了问题所在;线程(因此接受Deniz Mert Edincik的回答)。如果我从主线程的任何地方开始异步下载(因为基本上所有的TTImageView都是异步下载),它就无法启动。如果我在主线程上开始下载,它会立即启动..
6 个解决方案
#1
Sounds like a threading problem to me, are you creating TTImageView in the main runloop?
听起来像是一个线程问题,你在主runloop中创建TTImageView吗?
#2
I find one interesting thing. When I use combination TTTableViewController, TTTableViewDataSource and TTModel I have same problem with loading TTImageView. My problem was, that my implementation of Model methods 'isLoading' and 'isLoaded' don't return proper values after initialization of model. That forces me to call reload on model manualy in 'viewDidAppear' method and that causes image loading problem. So I repair my 'isLoading' and 'isLoaded' methods to both return 'NO' after Model init, and everything is fine.
我发现一件有趣的事情。当我使用组合TTTableViewController,TTTableViewDataSource和TTModel时,我在加载TTImageView时遇到同样的问题。我的问题是,模型初始化后,我的模型方法'isLoading'和'isLoaded'的实现没有返回正确的值。这迫使我在'viewDidAppear'方法中手动调用模型上的重载并导致图像加载问题。所以我修复了我的'isLoading'和'isLoaded'方法,以便在Model init之后返回'NO',一切都很好。
#3
When an image finishes loading try sending a reloadData message to the table view. This forces the table to recalculate the size of the rows and redraw the table. Just be careful that you don't start downloading the image again in response to this message.
当图像完成加载时,尝试将reloadData消息发送到表视图。这会强制表重新计算行的大小并重绘表。请注意,您不要再次开始下载图像以响应此消息。
#4
I've written something similar to this where an image view will load its own image from the web.
我写了类似的东西,图像视图将从网络加载自己的图像。
Im my experience, when the image had loaded successfully but was not shown in its view, it was a case that the cell needed to be told to redraw.
我的经验是,当图像成功加载但未在其视图中显示时,需要告知细胞需要重绘。
When you scroll the table view, the cells are set to redraw when the come onscreen, which is why they appear when you scroll.
当您滚动表格视图时,单元格设置为在屏幕上重绘时,这就是滚动时出现的原因。
When the image loads, tell the cell that it is sitting in to redraw by sending it the message setNeedsDisplay.
加载图像时,通过向其发送消息setNeedsDisplay告诉单元格它正在重绘。
That way, when the image finishes downloading, the cell its sitting in (and only that cell) will redraw itself to show the new image.
这样,当图像完成下载时,其所在的单元格(并且仅该单元格)将重绘自身以显示新图像。
It's possible that you might not need to redraw the entire cell and might be able to get away with simply redrawing the image view using the same method call. In my experience, my table cells view hierarchy was flattened, so I had to redraw the whole cell.
您可能不需要重绘整个单元格,并且可能只需使用相同的方法调用重新绘制图像视图即可。根据我的经验,我的表格单元格视图层次结构是扁平的,所以我不得不重绘整个单元格。
#5
I don't have an answer for what you want to do, but I will say that this is considered a feature, and the expected behavior. You use TTImageView in UITableView when you want to do lazy loading of images. TTImageView will only load the images whose frames are visible on the screen. That way, the device uses its network resources to download images that the user has in front of them, rather than a bunch of images that the user isn't even trying to look at.
我没有答案你想做什么,但我会说这被认为是一个功能,以及预期的行为。当您想要延迟加载图像时,可以在UITableView中使用TTImageView。 TTImageView仅加载帧在屏幕上可见的图像。这样,设备使用其网络资源下载用户在其前面的图像,而不是用户甚至不想看的一堆图像。
Consider having a long list that may contain a couple hundred thumbnail images (like a list of friends). I know from experience that if you kick off 100+ image requests on older devices, the memory usage will go through the roof, and your app will likely crash. TTImageView solves this problem.
考虑使用可能包含几百个缩略图图像的长列表(如朋友列表)。我从经验中知道,如果您在较旧的设备上启动100多个图像请求,内存使用量将会上升,您的应用程序可能会崩溃。 TTImageView解决了这个问题。
#6
This is a thread problem. You can load the images by including the line:
这是一个线程问题。您可以通过包含以下行来加载图像:
[TTURLRequestQueue mainQueue].suspended = NO;
in - (void)didLoadModel:(BOOL)firstTime
.
in - (void)didLoadModel:(BOOL)firstTime。
#1
Sounds like a threading problem to me, are you creating TTImageView in the main runloop?
听起来像是一个线程问题,你在主runloop中创建TTImageView吗?
#2
I find one interesting thing. When I use combination TTTableViewController, TTTableViewDataSource and TTModel I have same problem with loading TTImageView. My problem was, that my implementation of Model methods 'isLoading' and 'isLoaded' don't return proper values after initialization of model. That forces me to call reload on model manualy in 'viewDidAppear' method and that causes image loading problem. So I repair my 'isLoading' and 'isLoaded' methods to both return 'NO' after Model init, and everything is fine.
我发现一件有趣的事情。当我使用组合TTTableViewController,TTTableViewDataSource和TTModel时,我在加载TTImageView时遇到同样的问题。我的问题是,模型初始化后,我的模型方法'isLoading'和'isLoaded'的实现没有返回正确的值。这迫使我在'viewDidAppear'方法中手动调用模型上的重载并导致图像加载问题。所以我修复了我的'isLoading'和'isLoaded'方法,以便在Model init之后返回'NO',一切都很好。
#3
When an image finishes loading try sending a reloadData message to the table view. This forces the table to recalculate the size of the rows and redraw the table. Just be careful that you don't start downloading the image again in response to this message.
当图像完成加载时,尝试将reloadData消息发送到表视图。这会强制表重新计算行的大小并重绘表。请注意,您不要再次开始下载图像以响应此消息。
#4
I've written something similar to this where an image view will load its own image from the web.
我写了类似的东西,图像视图将从网络加载自己的图像。
Im my experience, when the image had loaded successfully but was not shown in its view, it was a case that the cell needed to be told to redraw.
我的经验是,当图像成功加载但未在其视图中显示时,需要告知细胞需要重绘。
When you scroll the table view, the cells are set to redraw when the come onscreen, which is why they appear when you scroll.
当您滚动表格视图时,单元格设置为在屏幕上重绘时,这就是滚动时出现的原因。
When the image loads, tell the cell that it is sitting in to redraw by sending it the message setNeedsDisplay.
加载图像时,通过向其发送消息setNeedsDisplay告诉单元格它正在重绘。
That way, when the image finishes downloading, the cell its sitting in (and only that cell) will redraw itself to show the new image.
这样,当图像完成下载时,其所在的单元格(并且仅该单元格)将重绘自身以显示新图像。
It's possible that you might not need to redraw the entire cell and might be able to get away with simply redrawing the image view using the same method call. In my experience, my table cells view hierarchy was flattened, so I had to redraw the whole cell.
您可能不需要重绘整个单元格,并且可能只需使用相同的方法调用重新绘制图像视图即可。根据我的经验,我的表格单元格视图层次结构是扁平的,所以我不得不重绘整个单元格。
#5
I don't have an answer for what you want to do, but I will say that this is considered a feature, and the expected behavior. You use TTImageView in UITableView when you want to do lazy loading of images. TTImageView will only load the images whose frames are visible on the screen. That way, the device uses its network resources to download images that the user has in front of them, rather than a bunch of images that the user isn't even trying to look at.
我没有答案你想做什么,但我会说这被认为是一个功能,以及预期的行为。当您想要延迟加载图像时,可以在UITableView中使用TTImageView。 TTImageView仅加载帧在屏幕上可见的图像。这样,设备使用其网络资源下载用户在其前面的图像,而不是用户甚至不想看的一堆图像。
Consider having a long list that may contain a couple hundred thumbnail images (like a list of friends). I know from experience that if you kick off 100+ image requests on older devices, the memory usage will go through the roof, and your app will likely crash. TTImageView solves this problem.
考虑使用可能包含几百个缩略图图像的长列表(如朋友列表)。我从经验中知道,如果您在较旧的设备上启动100多个图像请求,内存使用量将会上升,您的应用程序可能会崩溃。 TTImageView解决了这个问题。
#6
This is a thread problem. You can load the images by including the line:
这是一个线程问题。您可以通过包含以下行来加载图像:
[TTURLRequestQueue mainQueue].suspended = NO;
in - (void)didLoadModel:(BOOL)firstTime
.
in - (void)didLoadModel:(BOOL)firstTime。