图片下载的两种方式
来源:互联网 发布:云杉网络融资 编辑:程序博客网 时间:2024/05/23 00:06
图片下载的两种方式
//三方库下载
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];NSURL *URL = [NSURL URLWithString:@"http://www.scott-sherwood.com/wp-content/uploads/2013/01/scene.png"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
//NSLog(@"图片地址:%@",request);
UIApplication *app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible =YES;//转动
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response)
{
NSString *filenamepath=[NSHomeDirectory() stringByAppendingPathComponent:@"img/"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:filenamepath])//检查文件夹是否存在
{//文件夹存在
NSLog(@"文件夹存在");
}
else
{//文件夹不存在
NSLog(@"文件夹不存在");
//创建路径
[fileManager createDirectoryAtPath:filenamepath withIntermediateDirectories:YES attributes:nil error:nil];
}
NSURL *documentsDirectoryPath=[NSURL fileURLWithPath:filenamepath];
//返回你要保存的路径
return [documentsDirectoryPath URLByAppendingPathComponent:[response suggestedFilename]];
}completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error)
{
if(filePath==nil)
{
//图片地址空
NSLog(@"图片地址空,request:%@",request);
NSLog(@"error:%@",error);
}
else
{
NSLog(@"File downloaded to: %@", filePath);
}
}];
[downloadTask resume];
//普通下载
//创建保存图片文件夹
NSString *document = [NSHomeDirectory() stringByAppendingString:@"/Documents"];//找到沙盒中的Documents文件夹
NSFileManager *manager = [NSFileManager defaultManager];
BOOL isDirectory = [manager createDirectoryAtPath:[document stringByAppendingString:@"/pictureDownland"] withIntermediateDirectories:YES attributes:nil error:nil];//第一个参数是这次要创建的文件夹的完整路径
if(isDirectory)
{
NSLog(@"目录创建成功");
}
else
{
NSLog(@"目录创建失败");
}
//异步下载图片
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
Model *model = self.dataArr[current-1];
[img setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://ecy.cms.palmtrends.com%@",model.icon]]];
//获取当前时间
NSData *imagedata=UIImagePNGRepresentation(img.image);
NSDate *date = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];//一个日历对象
NSDateComponents *com = [[NSDateComponents alloc] init];//一个日历中元素的对象
//设置日历元素对象的集合,(选择你想要用的那些元素)
NSInteger flag = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit;
com = [calendar components:flag fromDate:date];//第一个参数就是上面做的元素对象的集合
int year = [com year];
int month = [com month];
int day = [com day];
int hour = [com hour];
int minute = [com minute];
int second = [com second];
//存储图片
[manager createFileAtPath:[[NSHomeDirectory() stringByAppendingString:@"/Documents/pictureDownland"] stringByAppendingString:[NSString stringWithFormat:@"/image_%d%d%d%d%d%d.png",year,month,day,hour,minute,second]] contents:imagedata attributes:nil]; //将图片保存为PNG格式
//提示
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"下载成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alert show];
break;
//单纯的提示框
#define AlertViewShow(content) UIAlertView *alerVier = [[UIAlertView alloc] initWithTitle:@"提示" message:content delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];\
[alerVier show];\
[alerVier release]
这个是alert的宏定义
- 图片下载的两种方式
- iOS个人封装图片下载:Block与Delegate两种方式
- Android 图片下载本地内存的缓存方式
- Android 图片下载本地内存的缓存方式
- 两种“两数互换的方式”
- Java实现网页截屏功能(图片下载功能)的几种方式(整理)
- include 的两种方式
- 抽象的两种方式
- 共享的两种方式
- 分页的两种方式
- tabhost的两种方式
- 归档的两种方式
- BroadcastReceiver的两种方式
- ajax 的两种方式
- 翻译的两种方式
- jni的两种方式
- 原型的两种方式
- 注入的两种方式
- 5 解析未定义的符号(下)
- _variant_t与相关类型之间的转换
- 剑指offer面七_用两个队列实现栈
- Cocoa 和 Objective-C 特性
- Android多线程机制解析
- 图片下载的两种方式
- mysql insertreplace update 的区别
- Duilib从Zip加载资源
- UITextView自定义文字属性后光标老是自动跳到末尾的问题
- Flex同Java通信--BlazeDS,通信失败
- js和jquery如何获取图片真实的宽度和高度
- 编写Android象棋游戏
- G代码介绍
- 开源项目源码解析-xUtils 源码解析