关于UIImageView缓存加载的笔记
加载图片的两个方法:
- [UIImage imageNamed:]
[[UIImage alloc] initWithContentsOfFile: imgpath]
[UIImage imageNamed:] : 加载的图片会自动缓存到内存中,不适合加载大图,内存紧张的时候可能会移除图片,需要重新加载,那么在界面切换的时候可能会引起性能下降
[[UIImage alloc] initWithContentsOfFile: imgpath]:加载图片,但未对图片进行解压,可以提前进行解压,提升加载速度.
//异步加载图片
CGSize imgViewS = imgView.bounds.size;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ), ^{ NSInteger index = indexPath.row;
NSString*imgpath = _imagePaths[index]; //绘制到context 提前解压图片
UIImage *img = [[UIImage alloc] initWithContentsOfFile: imgpath];
UIGraphicsBeginImageContextWithOptions(imgViewS , false, );
//这里也可以对图片进行压缩
[img drawInRect:CGRectMake(, , imgViewS.width, imgViewS.height)];
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//模拟延迟加载
[NSThread sleepForTimeInterval:]; dispatch_async(dispatch_get_main_queue(), ^{
//加载当前cell对应的图片
if (cell.tag == index) {
imgView.image = img;
NSLog(@"加载图片。。。。");
} });
二.由于每次加载都需要解压,每次解压都需要消耗内存,所以可以利用NSCahe缓存好加载过的图片
/**
利用NSCache缓存图片 */
- (UIImage*)loadImageIndex:(NSInteger)index {
static NSCache *cache = nil;
if (cache == nil) {
cache = [[NSCache alloc] init];
//最大缓存
// [cache setCountLimit:1024];
//每个最大缓存
// [cache setTotalCostLimit:1024];
}
UIImage *img = [cache objectForKey:@(index)];
if (img) {
return [img isKindOfClass:[NSNull class]]?nil:img;
}
//设置占位,防止图片多次加载
[cache setObject:[NSNull null] forKey:@(index)]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, ), ^{ NSString *imgpath = self.imagePaths[index];
UIImage *loadimg = [UIImage imageWithContentsOfFile:imgpath];
//渲染图片到contenx
UIGraphicsBeginImageContextWithOptions(loadimg.size, false, );
[loadimg drawAtPoint:CGPointZero]; loadimg = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();
dispatch_async(dispatch_get_main_queue(), ^{
//缓存图片
[cache setObject:loadimg forKey:@(index)]; NSIndexPath *indexpath = [NSIndexPath indexPathForRow:index inSection:];
UICollectionViewCell *cell = [self.collectView cellForItemAtIndexPath:indexpath];
if (cell != nil) {
UIImageView*imgV = [cell.contentView viewWithTag:];
imgV.image = loadimg;
} });
});
//未加载
return nil;
}
关于UIImageView缓存加载的笔记的更多相关文章
- Android批量图片加载经典系列——afinal框架实现图片的异步缓存加载
一.问题描述 在之前的系列文章中,我们使用了Volley和Xutil框架实现图片的缓存加载(查看系列文章:http://www.cnblogs.com/jerehedu/p/4607599.html# ...
- Expo大作战(十三)--expo如何自定义状态了statusBar以及expo中如何处理脱机缓存加载 offline support
简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...
- UIImageView异步加载网络图片
在iOS开发过程中,经常会遇到使用UIImageView展现来自网络的图片的情况,最简单的做法如下: 去下载https://github.com/rs/SDWebImage放进你的工程里,加入头文件# ...
- ios UIImageView异步加载网络图片
方法1:在UI线程中同步加载网络图片 UIImageView *headview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 4 ...
- unity3d 加密资源并缓存加载
原地址:http://www.cnblogs.com/88999660/archive/2013/04/10/3011912.html 首先要鄙视下unity3d的文档编写人员极度不负责任,到发帖为止 ...
- android 中使用缓存加载数据
最近app快完工了,但是很多列表加载,新闻咨询等数据一直从网络请求,速度很慢,影响用户体验,所以寻思用缓存来加载一些更新要求不太高的数据 废话不多说,上代码 欢迎转载,但请保留文章原始出处:) 博客 ...
- Android 开发 图片网络缓存加载框架Fresco
简介 Fresco是一个在Android应用程序中显示图像的强大系统. Fresco负责图像的加载和显示.它将从网络.本地存储或本地资源加载图像,图像加载完成前会显示一个占位图片.它有两个级别的缓存: ...
- u3d 加密资源并缓存加载
// C# Example // Builds an asset bundle from the selected objects in the project view. // Once compi ...
- ios UIImageView异步加载网络图片2
//1. NSData dataWithContentsOfURL // [self.icon setImage:[UIImage imageWithData:[NSData dataWithCont ...
随机推荐
- css3实现水平垂直居中------(特别注意,里边的固定还是不固定)
a,----定位方式(父元素宽高固定,子元素宽高固定) <div class="Father"> <div class="children"& ...
- 第2课第5节_Java面向对象编程_异常_P【学习笔记】
摘要:韦东山android视频学习笔记 java的异常处理的原则如下: 1.我们先写一个没有对异常处理的程序,在进行除法运算的时候,除数是非零的话,运行时没有问题的,但是除数为零的时候,运行就会有问 ...
- Linux中root用户找不到JAVA_HOME
Linux中root用户找不到JAVA_HOME 在Ubuntu环境中安装好Java环境后设置环境变量:在/etc/profile中设置好了JAVA_HOME变量并引入到PATH中,用于Ubunt ...
- Mxnet学习笔记(3)--自定义Op
https://blog.csdn.net/u011765306/article/details/54562282 前言 今天因为要用到tile操作(类似np.tile,将数据沿axises进行数据扩 ...
- Jmeter之测试计划
一.打开jmeter时会有一个测试计划默认显示,界面如下: 二.测试计划各个配置项说明 1.名称:即整个测试计划的名称,已实际项目命名为好: 2.注释:即添加一些备注信息,以便后期回顾时查看: 3.用 ...
- Global.asax.cs 为 /.aspx 执行子请求时出错。 Server.Transfer
x 后台代码 Global.asax.cs protected void Application_Error(object sender, EventArgs e){Server.Transfer(& ...
- spring boot 配置虚拟目录
如上图,关键地方有两个: 1.下方的 web.upload-path (配置本地文件路径) 2.上方一串配置,具体代码如下: profiles: include: paperIdentify acti ...
- alertmanager,grafana,prometheus
https://zhuanlan.zhihu.com/p/43637757 https://www.cnblogs.com/xiangsikai/p/11289966.html dashboard分文 ...
- [LeetCode] 407. Trapping Rain Water II 收集雨水 II
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- 【机器学习之二】python开发spark案例
环境 spark-1.6 python3.5 一.wordcount # -*- coding:utf-8 -*- ''' Created on 2019年5月13日 @author: Adminis ...