关于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 ...
随机推荐
- Spring Boot-IntelliJ IDEA搭建SpringBoot
点击create new project 点击next 这里基本都已经自动生成了,简单介绍下: Name:项目名称 Type:我们是Maven构建的,那么选择第一个Maven Project Pack ...
- 生成Nginx服务器SSL证书和客户端证书
Nginx服务器SSL证书 生成pass key 下面的命令用于生成一个2048bit的pass key, -passout pass:111111 用于避免交互式输入密码 [tomcat@a02 t ...
- String字符串与其他格式(int、boolean等八大数据类型)的相互转换
1.(String 转 int)String str = "123"; int a = Integer.parseInt(str);(int 转 String)int a = 5; ...
- 测量MySQL的表达式和函数的速度
测量MySQL的表达式和函数的速度,可以调用benchmark()函数.语法格式是benchmark(loop_count,expr).运行的返回值是0,但是mysql会打印一行显示语句大概要执行多长 ...
- 三层架构BLL+DAL+Model & MVC & MVVM
三层架构 - 国内版 Binghttps://cn.bing.com/search?FORM=U227DF&PC=U227&q=%E4%B8%89%E5%B1%82%E6%9E%B6% ...
- Maven构建报错问题解决
[ERROR] Failed to execute goal on project zepeto-admin: Could not resolve dependencies -SNAPSHOT: Fa ...
- apicloud打包成apk
前言:本文是打包vue项目,其他项目也是这样打包 页面的开发过程跟我们平时开发一样,利用vue把页面全部完成,最后进行npm run build将项目打包. 接下来就是apicloud打包的过程,首先 ...
- [Bayes] MCMC (Markov Chain Monte Carlo)
不错的文章:LDA-math-MCMC 和 Gibbs Sampling 可作为精进MCMC抽样方法的学习材料. 简单概率分布的模拟 Box-Muller变换原理详解 本质上来说,计算机只能生产符合均 ...
- postgrelsql 的 wm_concat : string_agg
string_agg,array_agg 这两个函数的功能大同小异,只不过合并数据的类型不同 array_agg(expression) 把表达式变成一个数组 一般配合 array_to_string ...
- [LeetCode] 675. Cut Off Trees for Golf Event 为高尔夫赛事砍树
You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-nega ...