六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 1421|回复: 0

IOS获取网络图片的方法

[复制链接]
 楼主| 发表于 2013-11-18 00:22:38 | 显示全部楼层 |阅读模式
通常来说,网络的的获取方法,是使用NSURL,NSURLRequest,NSURLConnection,这三个类,但是其实都是使用ASIHttp的第三方类,去获得网络信息。

具体的参考实例如下:

// 判断网络是否通
if ([[ReachabilityreachabilityForInternetConnection] currentReachabilityStatus]!=kNotReachable)
    {


        NSString *imagePath =@"http://www.weather.com.cn/m/i/weatherpic/29x20/d0.gif";// @"http://img.evolife.cn/2011-07/ad1e1823f6c67c75.jpg";


        NSURL *url = [NSURLURLWithString:imagePath];

        // 显示进度
        MBProgressHUD *loadingView = [[[MBProgressHUDalloc]initWithView:self.view]autorelease];
        loadingView.labelText = @"正在加载...";
        [self.view addSubview:loadingView];
        [loadingView setMode:MBProgressHUDModeIndeterminate];
        loadingView.taskInProgress = YES;
        [loadingView show:YES];


        // 直接获取,关键点是通过一个block实现,这个方法就不需要通过delegate去实现了。
        ASIHTTPRequest *httpRequest = [ASIHTTPRequestrequestWithURL:url];

        [httpRequest setCompletionBlock:^{

            [loadingView hide:YES];

            UIImage *downloadedImage = [UIImageimageWithData:[httpRequest responseData]];

            progressViewBlue.hidden =YES;

            imageView.image = downloadedImage;
        }];


        static longlong downloadedBytes = 0;

        [httpRequest setBytesReceivedBlock:^(unsignedlong long size, unsigned long long total){
            NSLog(@"size:%lld,total:%lld",size,total);
            downloadedBytes += size;
            CGFloat progressPercent = (CGFloat)downloadedBytes/total;

            loadingView.progress = progressPercent;


            progressViewBlue.progress = progressPercent;
            progressViewYellow.progress = progressPercent;
            progressLabel.text = [NSStringstringWithFormat"%.0f%%",progressPercent*100];

        }];


        [httpRequest startAsynchronous];



    }
    else
    {
        NSLog(@"network not available");
    }

这个项目涉及的第三方类:
1、Reachability.h,去判断网络是否通。
2、ASIHTTPRequest.h 请求。
3、MBProgressHUD.h  进度显示。
4、进度条,彩色的WNProgressView.h。

本文摘自:http://blog.csdn.net/dongdongdongjl/article/details/8496815

该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表