【代码笔记】iOS-显示图片的各种方式
代码:

- (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-显示图片的各种方式的更多相关文章
- 【代码笔记】iOS-使图片两边不拉伸,中间拉伸
代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // ...
- 【代码笔记】iOS-将图片处理成圆的
一,效果图. 二,工程图. 三,代码. ViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional ...
- 关于IOS显示图片的一些注意事项
1.-568h 的图片只能在def.png显示,其他的都不支持. 2.在加载图片到Images.xcassets里面的时候,如果图片下面出现Unassigned这个时候这个图片就不能用,得拿到Imag ...
- 【代码笔记】iOS-iOS图片的原生(Graphics)
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...
- 【代码笔记】iOS-gif图片播放
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...
- 代码: 两列图片瀑布流(一次后台取数据,图片懒加载。下拉后分批显示图片。图片高度未知,当图片onload后才显示容器)
代码: 两列图片瀑布流(一次后台取数据,无ajax,图片懒加载.下拉后分批显示图片.图片高度未知,当图片onload后才显示容器) [思路]: 图片瀑布流,网上代码有多种实现方式,也有各类插件.没找到 ...
- Android开发 从代码里设置Drawable图片不显示的问题
问题描述 我们从代码里获得Drawable在设置给View时会发现,图片不显示的问题.比如如下代码: Drawable drawable = getResources().getDrawable(R. ...
- iOS 正确选择图片加载方式
正确选择图片加载方式能够对内存优化起到很大的作用,常见的图片加载方式有下面三种: //方法1 UIImage *imag1 = [UIImage imageNamed:@"image.png ...
- 【OpenCV练习】简单显示图片的代码
今天依照网上的教程尝试了一下最基本的图片显示. 首先想说一下编译时出现的问题,开始时在编译时会出现无法识别cvReleaseImage的情况,是因为没有在配置中包含相应的core的库文件. 加进去就解 ...
- 【代码笔记】iOS-利用图片序列创建动态图片效果
一,效果图. 二,代码. RootViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional se ...
随机推荐
- 使用NVelocity生成内容的几种方式
使用NVelocity也有几个年头了,主要是在我的代码生成工具Database2Sharp上使用来生成相关代码的,不过NVelocity是一个非常不错的模板引擎,可以用来生成文件.页面等相关处理,非常 ...
- Mybatis中注解@MapKey的使用
在研究Mybatis源码之前并不知道这个注解的妙用的,但是当我看到参数解析的时候 有这个一个注解,所以我了解了一下,当我们返回像Map<String, Map<String, Object ...
- hibernate3
hibernate3 (整合到spring中的core核心配置中的hibernate3) <!-- 基于hibernate的Session工厂 --> <bean id=" ...
- collection中的retainAll()方法
public class ArraylistDemo { public static void main(String[] args) { Collection list1 = new ArrayLi ...
- jQUery操作checkbox
1 2 3 <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script> <s ...
- JS json的使用
json的定义 json能够通过4种基本数据类型以及2种结构化数据表示 字符串 "footbar" 不能使用单引号 数值 125.4 只支持10进制 布尔 true fals ...
- Guava学习笔记:Guava cache
缓存,在我们日常开发中是必不可少的一种解决性能问题的方法.简单的说,cache 就是为了提升系统性能而开辟的一块内存空间. 缓存的主要作用是暂时在内存中保存业务系统的数据处理结果,并且等待下次访问使用 ...
- Codrops 教程:实现内容倾斜的 3D 幻灯片效果
今天给大家分享的优秀教程来自 Codrops 网站,实现一个内容倾斜的 3D 幻灯片效果.我们平常见到的都是那种水平或者垂直滚动的效果,这个倾斜的内容滑动效果相信会让你眼前一亮.因为使用了 CSS 3 ...
- 【Bootstrap】4.企业网站(待续)
上一章有队个人站点站点进行一些优化.本章,轮到我们充实这个作品站点了,补充一些项目,从而展示我们的能力.话句话说,我们要构建一个相对复杂的企业网站主页. 下面有几个成功企业的网站: □ Zappos ...
- HTML 表单
HTML 表单包含表单元素. <form> 元素定义 HTML 表单 表单元素指的是不同类型的 input 元素.复选框.单选按钮.提交按钮等等. HTML 表单用于搜集不同类型的用户输入 ...