码迷,mamicode.com
首页 > 移动开发 > 详细

【iOS开发之旅】AFNetworking与SDWebImage下载图片

时间:2016-01-31 21:50:00      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

AFNetworking与SDWebImage下载图片

//
//  ViewController.m
//  AFNetworkingDemo
//
//  Created by ChenQianPing on 16/1/31.
//  Copyright © 2016年 chenqp. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"

// 原来是因为 iOS9之后,原http协议被改成了https协议,使用 TLS1.2 SSL加密请求数据。
#define imageUrl @"http://www.sanguosha.com/images/23/123/1107/26/12/9458_10132412.PNG"

@interface ViewController ()

@end

@implementation ViewController

// AFNetworking方式下载图片
// 获取一张网络图片,并加载到本地
// 网络下载数据,然后把数据转为图片,再加载
- (void)picture1
{
    UIImageView *iv = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:iv];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"image/png"];
    [manager GET:imageUrl parameters:nil success:^(AFHTTPRequestOperation *operation,id responseObject){
        NSLog(@"下载图片成功!");
        iv.image = [UIImage imageWithData:operation.responseData];
    }failure:^(AFHTTPRequestOperation *operation,NSError *error){
        NSLog(@"%@",error);
    }];
}

// SDWebImage方式下载图片
- (void)picture2
{
    UIImageView *iv = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:iv];
    [iv sd_setImageWithURL:[NSURL URLWithString:imageUrl]];
}


- (void)viewDidLoad {
    [super viewDidLoad];
//    [self picture1];
    [self picture2];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Info.plist设置
技术分享

【iOS开发之旅】AFNetworking与SDWebImage下载图片

标签:

原文地址:http://www.cnblogs.com/Bobby0322/p/5173883.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!