`
zcw_java
  • 浏览: 303707 次
  • 性别: 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

    iOS web view oc与js交互 图片浏览

    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrlString]]; UIImage *image = [UIImage imageWithData:imageData]; UIImageView *imageView = [[UIImageView alloc] initWithImage...

    ios 多线程下载图片并显示

    - (UIImage *)downloadImageFromUrl:(NSURL *)url { // 使用GCD异步下载图片 UIImage *image = nil; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData *data = ...

    获取UIWebVIew里的图片的demo

    UIImage *image = [UIImage imageWithData:imageData]; // 使用图片 } ``` 关于标签中的“canvas”,在某些情况下,图片可能存储在HTML5的Canvas元素内。这时,需要通过JavaScript的`toDataURL()`方法将Canvas...

    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 =

    oc-原生网络请求

    UIImage *image = [UIImage imageNamed:@"example.jpg"]; NSData *imageData = UIImageJPEGRepresentation(image, 0.8); // 质量为0.8的JPEG格式数据 NSString *urlString = @"http://yourserver.com/upload"; ...

    ios发送post请求(包括txt和image)的完整例子

    在Objective-C中,我们会用到`Foundation`框架中的`NSURLSession`和`NSData`类,以及`UIKit`框架中的`UIImage`类。确保在你的`.m`文件顶部有以下导入: ```objc #import #import ``` 接下来,我们将创建一个方法...

    实现简单的图片下载

    NSURL *url = [NSURL URLWithString:@"http://example.com/image.jpg"]; ``` 接下来,使用`NSURLRequest`来包装这个URL,以便`NSURLConnection`可以发送请求: ```objc NSURLRequest *request = [NSURLRequest ...

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

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:banarModel.avatar.url]]; UIImage *image = [UIImage imageWithData:data]; }); ``` 使用`dataWithContentsOfURL:`方法从指定URL下载图片...

    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 () 为要...

    IOS 图片上传处理 图片压缩 图片处理 --X枫林1

    - (NSData *)compressImage:(UIImage *)image withQuality:(CGFloat)quality { if (quality || quality &gt; 1.0) { quality = 0.8; // 设置默认压缩质量 } NSData *imageData = UIImageJPEGRepresentation(image...

    好用的第三方库AFNetworking

    UIImage *image = [UIImage imageNamed:@"image"]; NSData *imageData = UIImageJPEGRepresentation(image, 0.9); AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:...

    简单的图片上传

    NSData *imageData = UIImageJPEGRepresentation(image, 1.0); // 质量设为1.0,即最高质量 ``` 2. **构造请求参数**: 除了图片数据,可能还需要其他参数,如用户ID、图片标题等。这些参数可以以键值对的形式...

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

    NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:theURL]]; theImage = [[UIImage alloc] initWithData:imageData]; [BT_fileManager saveImageToFile:theImage fileName:...

    OC-performSelectorInBackground异步线程下载图片

    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]]; // 下载完成后,回到主线程更新UI dispatch_async(dispatch_get_main_queue(), ^{ imageView.image = image; }); } ``` 在...

    IOS 获取网络图片的大小

    - (void)lk_requestImageSizeFromURL:(NSURL *)url completion:(void(^)(CGSize size, NSError *error))completion { NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:...

Global site tag (gtag.js) - Google Analytics