加载图片的两个方法:

  • [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缓存加载的笔记的更多相关文章

  1. Android批量图片加载经典系列——afinal框架实现图片的异步缓存加载

    一.问题描述 在之前的系列文章中,我们使用了Volley和Xutil框架实现图片的缓存加载(查看系列文章:http://www.cnblogs.com/jerehedu/p/4607599.html# ...

  2. Expo大作战(十三)--expo如何自定义状态了statusBar以及expo中如何处理脱机缓存加载 offline support

    简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...

  3. UIImageView异步加载网络图片

    在iOS开发过程中,经常会遇到使用UIImageView展现来自网络的图片的情况,最简单的做法如下: 去下载https://github.com/rs/SDWebImage放进你的工程里,加入头文件# ...

  4. ios UIImageView异步加载网络图片

    方法1:在UI线程中同步加载网络图片 UIImageView *headview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 4 ...

  5. unity3d 加密资源并缓存加载

    原地址:http://www.cnblogs.com/88999660/archive/2013/04/10/3011912.html 首先要鄙视下unity3d的文档编写人员极度不负责任,到发帖为止 ...

  6. android 中使用缓存加载数据

    最近app快完工了,但是很多列表加载,新闻咨询等数据一直从网络请求,速度很慢,影响用户体验,所以寻思用缓存来加载一些更新要求不太高的数据 废话不多说,上代码 欢迎转载,但请保留文章原始出处:)  博客 ...

  7. Android 开发 图片网络缓存加载框架Fresco

    简介 Fresco是一个在Android应用程序中显示图像的强大系统. Fresco负责图像的加载和显示.它将从网络.本地存储或本地资源加载图像,图像加载完成前会显示一个占位图片.它有两个级别的缓存: ...

  8. u3d 加密资源并缓存加载

    // C# Example // Builds an asset bundle from the selected objects in the project view. // Once compi ...

  9. ios UIImageView异步加载网络图片2

    //1. NSData dataWithContentsOfURL // [self.icon setImage:[UIImage imageWithData:[NSData dataWithCont ...

随机推荐

  1. 如何开发一个npm包并发布到npm中央仓库

    转自: https://liaolongdong.com/2019/01/24/publish-public-npm.html 如何开发一个npm包并发布到npm中央仓库需求背景:平时在项目工作中可能 ...

  2. openresty开发系列34--openresty执行流程之4访问阶段

    openresty开发系列34--openresty执行流程之4访问阶段 访问阶段 用途:访问权限限制 返回403 nginx:allow 允许,deny 禁止 allow ip:deny ip: 涉 ...

  3. Python3基础 yield send 变量名= yield i

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  4. linux centos7 防止暴力破解

    系统 centos 7.4 系统, 不知道的可以用 cat /etc/redhat-release 查看 利用到了linux 系统的日志,每次我们登陆服务器时,如果有登陆认证失败的情况,会在服务器的/ ...

  5. Centos7安装图形界面桌面

    查看是否存在图形安装包.如果包含GNOME Desktop,则说明已存在. yum grouplist 安装图形化包 yum groupinstall "GNOME Desktop" ...

  6. [LeetCode] 749. Contain Virus 包含病毒

    A virus is spreading rapidly, and your task is to quarantine the infected area by installing walls. ...

  7. WARNING:Your password has expired --linux 用户密码过期

    今天在ssh 提示  WARNING:Your password has expired 设置用户到期时间 chage -M 36000 用户名 chage -l 用户名 #查看用户信息

  8. 通过 PECL 安装 PHP 扩展(以 CentOS7 中安装 swoole 为例)

    原文地址:https://blog.csdn.net/kikajack/article/details/82495190 常用工具PECL 和 phpize官网文档 PHP 有大量的扩展可以使用,比如 ...

  9. Influx Sql系列教程二:retention policy 保存策略

    retention policy这个东西相比较于传统的关系型数据库(比如mysql)而言,是一个比较新的东西,在将表之前,有必要来看一下保存策略有什么用,以及可以怎么用 I. 基本操作 1. 创建re ...

  10. linux 环境搭建

    1.安装jdk 上传tar包,解压缩 vim /etc/profile JAVA_HOME=/usr/local/jdk1.8.0_152CLASSPATH=$JAVA_HOME/lib/PATH=$ ...