Iphone UTableView随图像缓慢滚动
我加载Iphone UTableView随图像缓慢滚动,iphone,objective-c,uitableview,Iphone,Objective C,Uitableview,我加载UITableViewCell时遇到问题,显示了图像所有图像,但问题是uitableview滚动非常慢这是代码 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { customSearchesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DefaultC
UITableViewCell
时遇到问题,显示了图像
所有图像,但问题是uitableview
滚动
非常慢这是代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
customSearchesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DefaultCell"];
if(cell == nil)
{
cell =[[customSearchesCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DefaultCell"];
}
searchobjectval =[self.arrSearchResult objectAtIndex:indexPath.row];
cell.location.text =searchobjectval.location;
cell.title.text = searchobjectval.title;
cell.cost.text = searchobjectval.roomCost;
cell.desc.text =searchobjectval.description;
[tableView setSeparatorColor:[UIColor blueColor]];
UIView *myView = [[UIView alloc] init];
if (indexPath.row % 2)
{
UIColor* clr = [UIColor colorWithRed:0.4f green:0.6f blue:0.8f alpha:1];
myView.backgroundColor = clr;
}
else
{
myView.backgroundColor = [UIColor whiteColor];
}
cell.backgroundView = myView;
NSLog(@" search object image %@",searchobjectval.imgLink);
UIImage *ret = [self imageNamed:[NSString stringWithFormat: @"http://192.95.76.78/bedspace/new_arrivals_img/%@",searchobjectval.idVal ] cache:YES andsearchObj:searchobjectval];
cell.imgVw.image = ret;
return cell;
}
// images caching in dictionary
- (UIImage*)imageNamed:(NSString*)imageNamed cache:(BOOL)cache andsearchObj:(clsSearch *)searchObj
{
UIImage* retImage = [staticImageDictionary objectForKey:imageNamed];
@try
{
if (retImage == nil)
{
retImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageNamed]]];
if (cache)
{
if (staticImageDictionary == nil)
staticImageDictionary = [NSMutableDictionary new];
[staticImageDictionary setObject:retImage forKey:imageNamed];
}
}
}
@catch (NSException *exception)
{
NSLog(@"exception %@",exception);
}
@finally {
NSLog(@"finally block execute here");
}
return retImage;
}
请告诉我如何解决此问题,我已将代码粘贴到
if(cell==nil)
中,但尚未成功。问题在于将图像同步加载到tablecell中。使其异步,即可正常工作
用这个
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSString *strURL = url here;
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
UIImage *image = nil;
if(data)
image = [UIImage imageWithData:data];
dispatch_sync(dispatch_get_main_queue(), ^{
//now use image in image View or anywhere according to your requirement.
if(image)
yourImgView = image
});
});
您可以使用
SDWebImage
从Web服务器加载图像。retImage=[UIImage-imageWithData:[NSData-dataWithContentsOfURL:[NSURL-URLWithString:imageNamed]]代码>
对于出现的每个单元格,此行开始从服务器获取数据(图像)。当您开始滚动UITable
时,请求将转到服务器以获取图像,因此您应该使用NSThread
从服务器获取图像
请在这里尝试我的代码,如果有用,请向上投票。
请注意,方法:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageNamed]]代码>在主线程上运行
有关异步下载图像的信息,请参阅NSOperatioQueue Samople代码或ASIHTTP库。您可以使用SDWebImage库,它不仅可以异步加载图像,还可以使您的tableView滚动速度非常快
你可以从这里到图书馆 retImage=[UIImage-imageWithData:[NSData-dataWithContentsOfURL:[NSURL-URLWithString:imageNamed]]代码>:->异步执行此操作。在后台加载图像。单击。。。