iOS开发之旅AFNetworking与SDWebImage下载图片
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS开发之旅AFNetworking与SDWebImage下载图片相关的知识,希望对你有一定的参考价值。
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下载图片的主要内容,如果未能解决你的问题,请参考以下文章
IOS AFNetworking的使用与YYModel解析JSON数据