标签:
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