NSURL转NSData转UIImage


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;
}];

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值