代码:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view. UIImageView *img=[[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 200, 200)];
img.backgroundColor=[UIColor redColor];
[self.view addSubview:img]; //从网络下载图片,保存,并用 UIImageView 从保存中显示
NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSLog(@"保存路径:%@",documentsDirectoryPath); //Get Image From URL
UIImage * imageFromURL = [self getImageFromURL:@"http://file.duteba.com/phone/2009/04/5/ftGxL8kcUL.jpg"]; //Save Image to Directory
[self saveImage:imageFromURL withFileName:@"MyImage" ofType:@"jpg" inDirectory:documentsDirectoryPath]; //Load Image From Directory
UIImage * imageFromWeb = [self loadImage:@"MyImage" ofType:@"jpg" inDirectory:documentsDirectoryPath];
[img setImage:imageFromWeb]; //取得目录下所有文件名
NSArray *file = [[[NSFileManager alloc] init] subpathsAtPath:documentsDirectoryPath];
NSLog(@"%lu",(unsigned long)[file count]);
NSLog(@"%@",file); }
//从网络下载图片
-(UIImage *) getImageFromURL:(NSString *)fileURL {
NSLog(@"执行图片下载函数");
UIImage * result; NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
result = [UIImage imageWithData:data]; return result;
} //将所下载的图片保存到本地
-(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
if ([[extension lowercaseString] isEqualToString:@"png"]) {
[UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
} else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
[UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
} else {
//ALog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension);
NSLog(@"文件后缀不认识");
}
}
//读取本地保存的图片
-(UIImage *) loadImage:(NSString *)fileName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
UIImage * result = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.%@", directoryPath, fileName, extension]]; return result;
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

输出:

2015-10-13 13:43:09.989 从网络下载图片,保存,并用 UIImageView 从保存中显示[7260:182365] 保存路径:/Users/chenlihua/Library/Developer/CoreSimulator/Devices/78D1FCBF-9990-471F-9075-168F2CE949FE/data/Containers/Data/Application/52E9DF04-CC22-4834-A3C5-2261A5061032/Documents
2015-10-13 13:43:09.989 从网络下载图片,保存,并用 UIImageView 从保存中显示[7260:182365] 执行图片下载函数
2015-10-13 13:43:10.231 从网络下载图片,保存,并用 UIImageView 从保存中显示[7260:182365] 0
2015-10-13 13:43:10.231 从网络下载图片,保存,并用 UIImageView 从保存中显示[7260:182365] (
)

【代码笔记】iOS-显示图片的各种方式的更多相关文章

  1. 【代码笔记】iOS-使图片两边不拉伸,中间拉伸

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // ...

  2. 【代码笔记】iOS-将图片处理成圆的

    一,效果图. 二,工程图. 三,代码. ViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional ...

  3. 关于IOS显示图片的一些注意事项

    1.-568h 的图片只能在def.png显示,其他的都不支持. 2.在加载图片到Images.xcassets里面的时候,如果图片下面出现Unassigned这个时候这个图片就不能用,得拿到Imag ...

  4. 【代码笔记】iOS-iOS图片的原生(Graphics)

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

  5. 【代码笔记】iOS-gif图片播放

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

  6. 代码: 两列图片瀑布流(一次后台取数据,图片懒加载。下拉后分批显示图片。图片高度未知,当图片onload后才显示容器)

    代码: 两列图片瀑布流(一次后台取数据,无ajax,图片懒加载.下拉后分批显示图片.图片高度未知,当图片onload后才显示容器) [思路]: 图片瀑布流,网上代码有多种实现方式,也有各类插件.没找到 ...

  7. Android开发 从代码里设置Drawable图片不显示的问题

    问题描述 我们从代码里获得Drawable在设置给View时会发现,图片不显示的问题.比如如下代码: Drawable drawable = getResources().getDrawable(R. ...

  8. iOS 正确选择图片加载方式

    正确选择图片加载方式能够对内存优化起到很大的作用,常见的图片加载方式有下面三种: //方法1 UIImage *imag1 = [UIImage imageNamed:@"image.png ...

  9. 【OpenCV练习】简单显示图片的代码

    今天依照网上的教程尝试了一下最基本的图片显示. 首先想说一下编译时出现的问题,开始时在编译时会出现无法识别cvReleaseImage的情况,是因为没有在配置中包含相应的core的库文件. 加进去就解 ...

  10. 【代码笔记】iOS-利用图片序列创建动态图片效果

    一,效果图. 二,代码. RootViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional se ...

随机推荐

  1. UsefulSQL

    FindObject: ---在当前Server上找某某object,注意只需修改"要找的object"就可以使用EXEC sp_MSforeachdb 'use ? ;IF EX ...

  2. C#基础03

    学习集合的一些知识.集合:泛型集合,非泛型集合;ArrayList,Hashtable,List<T>,Dictionary<k,v>等,还有一些集合的常用方法. 一:集合的介 ...

  3. vue之自定义指令directive

    <template> <div> <input v-model="dir1" v-my-directive1="dir1"/> ...

  4. ASP.NET MVC文章附加有源码下载的文章

    很多一段时间以来,Insus.NET有分享很多有关ASP.NET MVC的文章,每隔一段时间,会把源码以及数据库分享供大家下载. 你可以按时间排序,文章越新,源码以及数据数据也就越新. 你可以从下面的 ...

  5. C#的变迁史 - C# 4.0篇

    C# 4.0 (.NET 4.0, VS2010) 第四代C#借鉴了动态语言的特性,搞出了动态语言运行时,真的是全面向“高大上”靠齐啊. 1. DLR动态语言运行时 C#作为静态语言,它需要编译以后运 ...

  6. 如何向非技术人(程序猿)解释SQL注入?

    前两天看博客园新闻,有一篇文章名为<我该如何向非技术人解释SQL注入?>(http://kb.cnblogs.com/page/515151/).是一个外国人写的,伯乐在线翻译的.我当时看 ...

  7. C#中使用Sql对Excel条件查询

    如何在C#中实现对Excel的条件查询呢? 在使用Sql条件语句对Excel进行查询时,遇到"至少一个参数没有被指定值"的问题,如何解决? 使用OleDbConnection对象创 ...

  8. mysql 常用

    create database jobs;grant all on jobs.* to root@'%' identified by '111111';flush privileges;

  9. javax.el.PropertyNotFoundException: Property 'name' not found on type java.lang.String

    javax.el.PropertyNotFoundException: Property 'name' not found on type java.lang.String javax.el.Bean ...

  10. 【转】定时自动启动任务crontab命令用法

    每个操作系统都有它的自动定时启动程序的功能,Windows有它的任务计划,而Linux对应的功能是crontab. crontab简介 crontab命令常见于Unix和类Unix的操作系统之中,用于 ...