`
zcw_java
  • 浏览: 297203 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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;
                           }];
    
}
分享到:
评论

相关推荐

    IOS开发中的各种Category

    NSData NSDate NSDictionary NSException NSFileManager NSObject NSSet NSString NSTimer NSURL UIKit UIBezierPath UIButton UIColor UIDevice UIImage UIImageView UILable UINavigationController UIResponder...

    ios-gitDemo.zip

    (UIImage *)animatedImageWithAnimatedGIFData:(NSData *)theData; url 图实现: (UIImage *)animatedImageWithAnimatedGIFURL:(NSURL *)theURL; github: https://github.com/His-writing/gitDemo

    HZExtensionKit:有用的类别

    HZ扩展类别UIColor , UIButton , UIAlertView , UIView , NSDate , NSFileManager , NSDictionary , NSString , UIImage , NSData , NSAttributedString , NSURL , NSArray 。 一些常见的宏和助手类。 ...

    IOS开发中加载大量网络图片优化方法

    IOS开发中加载大量网络图片如何优化 1、概述 在IOS下通过URL读一张网络图片并不像其他编程语言那样可以直接把图片路径放... NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]]; result =

    iOS 读取URL图片并存储到本地的实例

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:banarModel.avatar.url]]; UIImage *image = [UIImage imageWithData:data]; // 取得图片 // 本地沙盒目录 NSString *path = ...

    ios开发小技巧

    [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"POST"]; // NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"]; ...

    HanekeSwift:一个用Swift编写的iOS轻量级通用缓存,对图像特别钟爱

    Haneke是用Swift 4编写... Haneke为UIImage , NSData , JSON , String或可以作为数据读取或写入的任何其他类型提供内存和LRU磁盘缓存。 特别是Haneke擅长处理图像。 它包括一个具有自动调整大小的零配置图像缓存。

    HandsomeURLSession:适用于iOS,watchOS,tvOS和macOS的NSURLSession扩展

    数据: NSData 图片: UIImage或NSImage XML: NSXMLParser 可可豆 使用将安装到您的项目中。 pod 'HandsomeURLSession' ##用法 获取一个NSURLSession let session = NSURLSession. sharedSession () 为要...

    Objective C从远程url下载图片方法汇总

    Objective C从远程url下载图片 - (UIImage *) getImageFromURL: (NSString *)... NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:theURL]]; theImage = [[UIImage alloc] initWit

    XWDatabase - 市面上最易用的数据库工具类

    long,signed,float,double,NSInteger,CGFloat,BOOL,NSString,NSMutableString,NSNumber,NSArray,NSMutableArray,NSDictionary,NSMutableDictionary,NSData,NSMutableData,UIImage,NSDate,NSURL,NSRange,CGRect,...

Global site tag (gtag.js) - Google Analytics