代码:

- (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. C# 通过反射来动态创建泛型类型

    C# 通过反射来动态创建泛型类型与创建普通类型的区别在于:泛型参数的处理 创建泛型类型存在三种情况: 第一种:知道泛型类型,但需要动态指定泛型参数: 第二种:知道泛型参数,但需要动态创建指定参数的泛型 ...

  2. codeMirror插件使用讲解

    codeMirror是一款十分强大的代码编辑插件,提供了十分丰富的API,最近在项目中用到了这款插件,于是在这里给大家分享下使用方法和心得: codeMirror调用非常方便 首先在页面中载入插件CS ...

  3. EF基本操作增、删、查、改、分页,join……等

    一.批量添加数据 static void Main(string[] args) { add(); add2(); Console.ReadKey(); } static void add() { D ...

  4. 使用ASP.NET MVC局部视图避免JS拼接HTML,编写易于维护的HTML页面

    以前使用ASP.NET WebForm开发时,喜欢使用Repeater控件嵌套的方式开发前台页面,这样就不用JS拼接HTML或者后台拼接HTML了,写出的HTML页面美观.简捷.易于维护,由于不用JS ...

  5. 常用Eclipse快捷方式

    Ctrl+1 快速修复 Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制当前行到上一行(复制增加) Alt+↓ 当前行和下面一行交互位置(特 ...

  6. 设置MySQL服务自动运行

    一般情况下,MySQL安装以后是自动运行的,不知道我这台机器是什么原因,MySQL不能自动运行,每次开机后都要手动运行mysqld.exe,比较麻烦,于是用以下方法将MySQL自动启动: 1. 运行c ...

  7. Android退出时关闭所有Activity的方法

    Android退出时,有的Activity可能没有被关闭.为了在Android退出时关闭所有的Activity,设计了以下的类: //关闭Activity的类 public class CloseAc ...

  8. Delphi 10 Seattle Update 1 修复 iOS HTTP 协定需求

    在 iOS 9 Apple 加入了 HTTP 协议,还好有 TMS 提供快速修复工具,得以能顺利上架到 App Store. 现在 Delphi 10 Seattle Update 1 提供了这个设定 ...

  9. 动态加载HTML后使用query修改标签样式

    下面的IMG 标签的宽度从后台返回是10PX,加载完毕后,修改成100PX,注意:拼接的代码在 body标签之后,或则直接在HTML外面增加也可以 <html> <head> ...

  10. 框架SpringMVC笔记系列 一 基础

    主题:SpringMVC 学习资料参考网址: 1.http://www.icoolxue.com 2.http://aokunsang.iteye.com/blog/1279322 1.SpringM ...