Mas*_*oke 2 iphone xcode objective-c ios ios5
我试图以下列方式加载图像.但是当我调用我的loadImageFromURL:(NSURL*)inURL函数时[self loadImagesFromURL:url],我tableView的显示0字节.如何获得(delta)NSData我的tableview?的价值,被宣布为全球...
NSURLConnection* connection;
NSMutableData* delta;
- (void)loadImageFromURL:(NSURL*)inURL {
NSURLRequest *request = [NSURLRequest requestWithURL:inURL];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
if (conn) {
delta = [[NSMutableData data] retain];
}
}
- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data {
[delta appendData:data];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSURL *urlink=[NSURL URLWithString:[[objectsForImages objectForKey:[arrayOfCharacters objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]];
[self loadImageFromURL:urlink];
UIImage *imageForAz=[UIImage imageWithData:delta];
cell.imageView.image=imageForAz;
}
Run Code Online (Sandbox Code Playgroud)
你对异步性感到困惑.如果loadImageFromURL:是同步的,例如包装sendSynchronousRequest:returningResponse:error:,您的代码将按预期工作; loadImageFromURL:在获取时会阻塞,并在delta填充时返回.
但是,这都是异步的,所以你真正需要做的是connectionDidFinishLoading:在你的委托中实现(在本例中是self)并在那里设置cell.imageView.image它.
相应地重写一些代码(我假设你tableView:cellForRowAtIndexPath:为了这个例子的目的删除了无关的代码):
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSURL *urlink=[NSURL URLWithString:[[objectsForImages objectForKey:[arrayOfCharacters objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row]];
[self loadImageFromURL:urlink];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
UIImage *imageForAz=[UIImage imageWithData:delta];
cell.imageView.image=imageForAz;
}
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息/示例,请查看NSURLConnection上的URL加载系统编程指南.
| 归档时间: |
|
| 查看次数: |
858 次 |
| 最近记录: |