NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSString *fileURL = [NSString stringWithFormat:@"http://%@/image/%@/show.jpg?n=%d", 1,1]; NSData *dateImg = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]]; dispatch_sync(dispatch_get_main_queue(), ^{ m_imgViewVideo.image = [UIImage imageWithData:dateImg]; if (![ABServices bIsPreview]) { m_nPreviewCount = 0; } }); [pool drain];
dispatch_sync:提交块对象给指定的调剂队列,同步履行。
dispatch_async:提交块对象给指定的调剂队列,异步履行。
dispatch_async() 调用以后立即返回,dispatch_sync() 调用以后等到block执行完以后才返回,dispatch_sync()会阻塞当前线程。
//访问图片时响应404的没获取到数据时,判断code,0为ok
NSString *strURL = [NSString stringWithFormat:kUrlReadCameraAlarmInfo,[ABServices currentUserName],strSubClass];
NSError *error = nil;
NSData *dataImg = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL] options:NSDataReadingMappedIfSafe error:&error];
if ([error code] == 0)
{
//截取图片压缩
UIImage *imgCompress = [self image:[UIImage imageWithData:dataImg] centerInSize:CGSizeMake(140, 140)];
NSData *dataCompress = UIImageJPEGRepresentation(imgCompress, 1.0);
[self saveImage:dataCompress nIndex:indexPath.row picName:strSubClass];
}
以下是在tableview中全部代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"cate_cell"; CateTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[CateTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; cell.selectionStyle = UITableViewCellSelectionStyleNone; } //cell默认图片 UIImageView *imgDefault = [[UIImageView alloc] init]; imgDefault.image = [self image:[UIImage imageNamed:@"alarm_icon.png"] centerInSize:CGSizeMake(140, 140)]; //获取每条报警记录 NSDictionary *cate = [m_mutArrAlarmPicList objectAtIndex:indexPath.row]; NSString *strImgPath = [NSString stringWithFormat:@"%@_0.jpg",[cate objectForKey:@"alarmPic"]]; //搜索缓存 UIImage *image = [self searchCacheImg:strImgPath nCellIndexPath:indexPath.row]; if(image != nil) { imgDefault.image = image; } else { dispatch_async(dispatch_get_global_queue(0, 0), ^{ NSString *strURL = [NSString stringWithFormat:kUrlReadCameraAlarmInfo,[ABServices currentUserName],strImgPath]; NSError *error = nil; NSData *dataImg = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL] options:NSDataReadingMappedIfSafe error:&error]; if ([error code] == 0) { //截取图片压缩 UIImage *imgCompress = [self image:[UIImage imageWithData:dataImg] centerInSize:CGSizeMake(140, 140)]; NSData *dataCompress = UIImageJPEGRepresentation(imgCompress, 1.0); [self saveImage:dataCompress nIndex:indexPath.row picName:strImgPath]; dispatch_sync(dispatch_get_main_queue(), ^{ imgDefault.image = [UIImage imageWithData:dataCompress]; NSArray *arrRows = [NSArray arrayWithObjects:indexPath, nil]; [_tableView reloadRowsAtIndexPaths:arrRows withRowAnimation:UITableViewRowAnimationNone]; }); } }); } cell.logo.image = imgDefault.image; [imgDefault release]; cell.title.text = [self stringFormatDate:[cate objectForKey:@"alarmPic"]]; cell.subTtile.text = [NSString stringWithFormat:@"%@",[cate objectForKey:@"alarmSensor"]]; return cell; }
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { SubCateViewController *subVc = [[[SubCateViewController alloc] initWithNibName:NSStringFromClass([SubCateViewController class]) bundle:nil] autorelease]; NSDictionary *cate = [m_mutArrAlarmPicList objectAtIndex:indexPath.row]; subVc.m_nIndexPathRow = indexPath.row; //保存选中cell索引 m_nSelectedCellIndex = indexPath.row; NSMutableArray *mutArrPics = [[NSMutableArray alloc] init]; for (int i=0; i < 10; i++) { NSString *strSubClass = [NSString stringWithFormat:@"%@_%d.jpg",[cate objectForKey:@"alarmPic"],i]; [mutArrPics addObject:strSubClass]; dispatch_async(dispatch_get_global_queue(0, 0), ^{ //搜索图片是否存在 UIImage *image = [self searchCacheImg:strSubClass nCellIndexPath:indexPath.row]; if(image == nil) { NSString *strURL = [NSString stringWithFormat:kUrlReadCameraAlarmInfo,[ABServices currentUserName],strSubClass]; NSError *error = nil; NSData *dataImg = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL] options:NSDataReadingMappedIfSafe error:&error]; if ([error code] == 0) { //截取图片压缩 UIImage *imgCompress = [self image:[UIImage imageWithData:dataImg] centerInSize:CGSizeMake(140, 140)]; NSData *dataCompress = UIImageJPEGRepresentation(imgCompress, 1.0); [self saveImage:dataCompress nIndex:indexPath.row picName:strSubClass]; } } }); } subVc.subCates = mutArrPics; [mutArrPics release]; // self.currentCate = cate; subVc.cateVC = self; self.tableView.scrollEnabled = NO; UIFolderTableView *folderTableView = (UIFolderTableView *)tableView; [folderTableView openFolderAtIndexPath:indexPath WithContentView:subVc.view openBlock:^(UIView *subClassView, CFTimeInterval duration, CAMediaTimingFunction *timingFunction){ // opening actions } closeBlock:^(UIView *subClassView, CFTimeInterval duration, CAMediaTimingFunction *timingFunction){ // closing actions } completionBlock:^{ // completed actions self.tableView.scrollEnabled = YES; }]; }